0% found this document useful (0 votes)
29 views

CSE Module 1

Uploaded by

motoh6220
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CSE Module 1

Uploaded by

motoh6220
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Object-Oriented Programming Fundamentals

Dr. S Sudheer Mangalampalli


Assistant Professor, Senior Grade1
School of Computer Science and Engineering
VIT-AP University

April 12, 2022

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
1 / 85
Differences between Procedural Oriented and Object
Oriented Programming

Procedural Oriented Programming


• Program is divided into small parts called as functions.
• It follows top down approach
• No access specifiers in this paradigm.
• Adding new data and function is not easy
• It is not having proper data hiding mechanism so that it is less secure.
• In this model, function is more important than data.
• Ex: C, FORTRAN, PASCAL etc..

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
2 / 85
Differences between Procedural Oriented and Object
Oriented Programming

Object-Oriented Programming
• In this model, Program is divided into small parts called Objects.
• It follows bottom up approach.
• It have access specifiers like Public, Private and Protected.
• Adding new data and functions is easy.
• It is more secure
• Overloading is possible in this paradigm.
• In this model, data is more important than function.
• Ex: C++, Java, Python etc.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
3 / 85
Objects and Classes in Java

What is an Object in Java


• A Java object is a member (also called an instance) of a Java class.
Each object has an identity, a behavior and a state.
• The state of an object is stored in fields (variables), while methods
(functions) display the object’s behavior.
• Objects are created at runtime from templates, which are also known
as classes.
• Java objects are very similar to the objects we can observe in the real
world.
• Ex: A cat, a lighter, a pen, or a car are all objects.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
4 / 85
What is Class in Java

• A class is a template, blueprint,or contract that defines what an


object’s data fields and methods.
• An object is an instance of a class.
• We can create many instances of a class.
• A Java class uses variables to define data fields and methods to define
actions.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
5 / 85
Features of OOPS

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
6 / 85
Four main Pillars of OOPs

• Abstraction
• It is a process of hiding implementation details and exposes only the
functionality to the user.
• In abstraction, we deal with ideas and not events.
• This means the user will only know “what it does” rather than “how it
does”.
Real-Life Example- A driver will focus on the car functionality (Start or
Stop, Accelerate or Break), he or she does not bother about how the
Accelerate or brake mechanism works internally. And this is how the
abstraction works.
• Encapsulation is the process of wrapping code and data together
into a single unit.
Real-Life Example: A capsule which is mixed of several medicines. The
medicines are hidden data to the end user.
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
7 / 85
Four main Pillars of OOPs

• Inheritance is the process of one class inheriting properties and


methods from another class in Java.
• Inheritance is used when we have is-a relationship between objects.
• Inheritance in Java is implemented using extends keyword.
Real-life Example:The planet Earth and Mars inherits the super class
Solar System and Solar system inherits the Milky Way Galaxy. So Milky
Way Galaxy is the top super class for Class Solar System, Earth and Mars.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
8 / 85
Four Main Pillars of OOPs

• Polymorphism is the ability to perform many things in many ways.


• The word Polymorphism is from two different Greek words- poly and
morphs.
• “Poly” means many, and “Morphs” means forms. So polymorphism
means many forms.
Real-life Example:A delivery person delivers items to the user. If it’s a
postman he will deliver the letters. If it’s a food delivery boy he will deliver
the foods to the user. Like this polymorphism implemented different ways
for the delivery function.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
9 / 85
Execution of a Java Program

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
10 / 85
Structure of a Java Program

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
11 / 85
Documentation Section
• It is an important section but optional for a Java program.
• The comments may be single-line, multi-line, and documentation
comments.
Single-line Comment It starts with a pair of forwarding slash (//)
// H e l l o S a i
Multi-line CommentIt starts with a /* and ends with */. We write
between these two symbols.
/∗ I t i s an e x a m p l e o f
m u l t i l i n e comment ∗/
Documentation CommentIt starts with the delimiter (/**) and ends
with */.
/∗∗ I t i s an e x a m p l e o f d o c u m e n t a t i o n comment ∗/
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
12 / 85
Package Section

• The package declaration is optional.It is placed just after the


documentation section.
• In this section, we declare the package name in which the class is
placed.
• Note that there can be only one package statement in a Java
program. It must be defined before any class and interface
declaration.
package sudheer ;

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
13 / 85
Interface Section

• The package contains the many predefined classes and interfaces. If


we want to use any class of a particular package, we need to import
that class.
• The import statement represents the class stored in the other
package.
• We use the import keyword to import the class. It is written before
the class declaration and after the package statement.
• We use the import statement in two ways, either import a specific
class or import all classes of a particular package.
import java . u t i l . Scanner ;
import java . u t i l . ∗ ;

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
14 / 85
Interface Section

• It is an optional section. We can create an interface in this section if


required.
• We use the interface keyword to create an interface.
• An interface is a slightly different from the class. It contains only
constants and method declarations.
• Another difference is that it cannot be instantiated.We can use
interface in classes by using the implements keyword.
i n t e r f a c e car
{
void s t a r t ( ) ;
void stop ( ) ;
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
15 / 85
Class Definition
• In this section, we define the class. It is vital part of a Java program.
• Without the class, we cannot create any Java program.
• A Java program may conation more than one class definition. We use
the class keyword to define the class.
• The class is a blueprint of a Java program. It contains information
about user-defined methods, variables, and constants.
• Every Java program has at least one class that contains the main()
method.
c l a s s S t u d e n t // c l a s s d e f i n i t i o n
{
S t r i n g sname ; // v a r i a b l e
int id ;
double percentage ;
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
16 / 85
Main Method Class

1 In this section, we define the main() method.


2 It is essential for all Java programs. Because the execution of all Java
programs starts from the main() method.
3 In other words, it is an entry point of the class. It must be inside the
class.
4 Inside the main method, we create objects and call the methods.

p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
17 / 85
Java main method

public static void main(String[] args)


1 It is the most important java method. When you start learning java
programming, this is the first method you encounte.
2 public- This is the access modifier of the main method. It has to be
public so that java runtime can execute this method.
p u b l i c c l a s s Test {

s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
18 / 85
Java main method
static
• When java runtime starts, there is no object of the class present.
That’s why the main method has to be static so that JVM can load
the class into memory and call the main method.
• If the main method won’t be static, JVM would not be able to call it
because there is no object of the class is present.

p u b l i c c l a s s Test {

p u b l i c v o i d main ( S t r i n g [ ] a r g s ) {

System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
19 / 85
Java main method

void
• Java programming mandates that every method provide the return
type.
• ava main method doesn’t return anything, that’s why it’s return type
is void.
p u b l i c c l a s s Test {

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {

return 0;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
20 / 85
Java main method

main
• This is the name of java main method.
• It’s fixed and when we start a java program, it looks for the main
method.

p u b l i c c l a s s Test {

p u b l i c s t a t i c v o i d mymain ( S t r i n g [ ] a r g s )
{

System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
21 / 85
Java main method

• Java main method accepts a single argument of type String array.


• This is also called as java command line arguments.

p u b l i c c l a s s Test {

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {

f o r ( S t r i n g s : a r g s ){
System . o u t . p r i n t l n ( s ) ;
}

}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
22 / 85
Java Variables

• Variables are containers for storing data values.


• In Java, there are different types of variables
• String - stores text, such as ”Hello”. String values are surrounded by
double quotes
• int- stores integers (whole numbers), without decimals, such as 123
or -123
• float- stores floating point numbers, with decimals, such as 19.99 or
-19.99
• char - stores single characters, such as ’a’ or ’B’. Char values are
surrounded by single quotes
• boolean - stores values with two states: true or false

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
23 / 85
Declaring (Creating) Variables
• To create a variable, you must specify the type and assign it a value:
type variableName = value ;
• Where type is one of Java’s types (such as int or String), and
variableName is the name of the variable (such as x or name).
• The equal sign is used to assign values to the variable.
To create a variable that should store text, look at the following example:
S t r i n g name = ” John ” ;
System . o u t . p r i n t l n ( name ) ;
To create a variable that should store a number, look at the following
example:
i n t myNum = 1 5 ;
System . o u t . p r i n t l n (myNum ) ;
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
24 / 85
Java Variables

You can also declare a variable without assigning the value, and
assign the value later:
i n t myNum ;
myNum = 1 5 ;
System . o u t . p r i n t l n (myNum ) ;
Note that if you assign a new value to an existing variable, it will
overwrite the previous value:
i n t myNum = 1 5 ;
myNum = 2 0 ; // myNum i s now 20
System . o u t . p r i n t l n (myNum)

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
25 / 85
Java variables
To declare more than one variable of the same type, you can use a
comma-separated list
i n t x = 5 , y = 6 , z = 50;
System . o u t . p r i n t l n ( x + y + z ) ;
One Value to Multiple Variables
int x , y , z ;
x = y = z = 50;
System . o u t . p r i n t l n ( x + y + z ) ;
A demonstration of how to declare variables of other types:
i n t myNum = 5 ;
f l o a t myFloatNum = 5 . 9 9 f ;
c h a r m y L e t t e r = ’D ’ ;
b o o l e a n myBool = t r u e ;
S t r i n g myText = ” H e l l o ” ;
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
26 / 85
General rules for writing Java Variables

1 Names can contain letters, digits, underscores, and dollar signs


2 Names must begin with a letter
3 Names should start with a lowercase letter and it cannot contain
whitespace
4 Names can also begin with dollar and underscore
5 Names are case sensitive (”myVar” and ”myvar” are different
variables)
6 Reserved words (like Java keywords, such as int or boolean) cannot
be used as names

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
27 / 85
Java Data types

Data types are divided into two groups:


Primitive data types - includes byte, short, int, long, float, double,
boolean and char
Non-primitive data types - such as String, Arrays and Classes
Data type Size Description
byte 1 byte Whole no’s
short 2 bytes Whole no’s
int 4 bytes Whole no’s
long 8 bytes Whole no’s
float 4 bytes Stores fractional numbers.
double 8 bytes Stores fractional numbers.
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
28 / 85
Java Operators

Arithmetic Operators

Name Description
Addition(+) Adds together two values
Subtraction(-) Subtracts one value from another
Multiplication(*) Multiplies two values
Division(/) Divides one value by another
Modulus Returns the division remainder
Increment(++) Increases the value of a variable by 1
Decrement(- -) Decreases the value of a variable by 1

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
29 / 85
Java Operators

Assignment Operators

Operator Example
= x=5
+= x+=3
-= x-=3
*= x*=3
/= x/=3

Example
i n t x = 10;
x += 5 ;

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
30 / 85
Java Operators

Comparison Operators

Operator Example
== x == y
!= x != y
> x>y
< x <y
>= x> = y
<= x<=y

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
31 / 85
Java Operators

Java Logical Operators

Name Description
Logical and Returns true if both statements are true
Logical OR Returns true if one of the statements is true
Logical NOT Reverse the result, returns false if the result is true

x < 5 && x < 10

x < 5 || x < 4

! ( x < 5 && x < 1 0 )

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
32 / 85
Java Classes and Objects

Object-oriented programming has several advantages over procedural


programming
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the Java code DRY ”Don’t Repeat Yourself”, and
makes the code easier to maintain, modify and debug.
• OOP makes it possible to create full reusable applications with less
code and shorter development time

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
33 / 85
Java - What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented


programming

A class is a template for objects, and an object is an instance of a class.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
34 / 85
classes and Objects

Everything in Java is associated with classes and objects, along with its
attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
create a class
p u b l i c c l a s s Main {
int x = 5;
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
35 / 85
Create an Object in Java
In Java, an object is created from a class.
Syntax for creation of an Object in Java
ClassName o b j e c t = new ClassName ( ) ;
To create an object of Main, specify the class name, followed by the object
name, and use the keyword new
Example
p u b l i c c l a s s Main {
int x = 5;

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
36 / 85
Multiple Objects

You can create multiple objects of one class


Example
p u b l i c c l a s s Main {
int x = 5;

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj1 = new Main ( ) ; // O b j e c t 1
Main myObj2 = new Main ( ) ; // O b j e c t 2
System . o u t . p r i n t l n ( myObj1 . x ) ;
System . o u t . p r i n t l n ( myObj2 . x ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
37 / 85
Using Multiple Classes
• You can also create an object of a class and access it in another class.
• This is often used for better organization of classes (one class has all
the attributes and methods, while the other class holds the main()
method (code to be executed)).
Main.java
p u b l i c c l a s s Main {
int x = 5;
}
Second.java
c l a s s Second {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
}
}Sudheer Mangalampalli Assistant Professor,
Dr. S Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
38 / 85
Java class Attributes
Example for declaration of variables in class
p u b l i c c l a s s Main {
int x = 5;
int y = 3;
}
Accessing Attributes The following example will create an object of the
Main class, with the name myObj.
We use the x attribute on the object to print its value:
p u b l i c c l a s s Main {
int x = 5;

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
} Mangalampalli Assistant Professor,
Dr. S Sudheer Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
39 / 85
Modify attributes

Example
p u b l i c c l a s s Main {
i n t x = 10;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
myObj . x = 2 5 ; // x i s now 25
System . o u t . p r i n t l n ( myObj . x ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
40 / 85
Attributes contd..

If you don’t want the ability to override existing values, declare the
attribute as final
Example
p u b l i c c l a s s Main {
f i n a l i n t x = 10;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
myObj . x = 2 5 ; // w i l l g e n e r a t e an e r r o r : c a n n o t a s
System . o u t . p r i n t l n ( myObj . x ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
41 / 85
Multiple Objects
If you create multiple objects of one class, you can change the attribute
values in one object, without affecting the attribute values in the other
Example
Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:
p u b l i c c l a s s Main {
int x = 5;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
Main myObj1 = new Main ( ) ; // Object 1
Main myObj2 = new Main ( ) ; // Object 2
myObj2 . x = 2 5 ;
System . o u t . p r i n t l n ( myObj1 . x ) ; // O u t p u t s 5
System . o u t . p r i n t l n ( myObj2 . x ) ; // O u t p u t s 25
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
42 / 85
Multiple Attributes

You can specify as many attributes as you want Example


p u b l i c c l a s s Main {
S t r i n g fname = ” John ” ;
S t r i n g lname = ”Doe ” ;
i n t age = 2 4 ;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( ” Name : ” + myObj . fname + ” ” + myO
System . o u t . p r i n t l n ( ” Age : ” + myObj . age ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
43 / 85
Java Methods

Example Create a method named myMethod() in Main:


p u b l i c c l a s s Main {
s t a t i c v o i d myMethod ( ) {
System . o u t . p r i n t l n ( ” H e l l o World ! ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
44 / 85
Static vs Non Static Methods
p u b l i c c l a s s Main {
// S t a t i c method
s t a t i c v o i d mySta ticM etho d ( ) {
System . o u t . p r i n t l n ( ” S t a t i c methods ” ) ;
}
// P u b l i c method
p u b l i c v o i d myPublicMethod ( ) {
System . o u t . p r i n t l n ( ” P u b l i c methods ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
my StaticMethod ( ) ; // C a l l t h e s t a t i c method
Main myObj = new Main ( ) ;
myObj . myPublicMethod ( ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
45 / 85
Access Methods With an Object

p u b l i c c l a s s Main {
public void f u l l T h r o t t l e () {
System . o u t . p r i n t l n ( ” The c a r i s g o i n g f a s t ” ) ;
}
p u b l i c v o i d s p e e d ( i n t maxSpeed ) {
System . o u t . p r i n t l n ( ” Max s p e e d i s : ” + maxSpeed ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myCar = new Main ( ) ;
myCar . f u l l T h r o t t l e ( ) ;
myCar . s p e e d ( 2 0 0 ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
46 / 85
Java Constructors

A constructor in Java is a special method that is used to initialize objects.


The constructor is called when an object of a class is created.
It can be used to set initial values for object attributes.
p u b l i c c l a s s Main {
int x ;
p u b l i c Main ( ) {
x = 5;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
47 / 85
Java Constructors contd..
Note that the constructor name must match the class name, and it cannot
have a return type (like void).
Also note that the constructor is called when the object is created.
Constructor Parameters
p u b l i c c l a s s Main {
int x ;
p u b l i c Main ( i n t y ) {
x = y;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( 5 ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
48 / 85
Java Constructors contd..
You can have as many parameters as you want in constructors
Example
p u b l i c c l a s s Main {
i n t modelYear ;
S t r i n g modelName ;
p u b l i c Main ( i n t y e a r , S t r i n g name ) {
modelYear = y e a r ;
modelName = name ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myCar = new Main ( 1 9 6 9 , ” Mustang ” ) ;
System . o u t . p r i n t l n ( myCar . modelYear + ” ”+myCar . modelNa
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
49 / 85
Access Specifiers for classes
public- The class is accessible by any other class
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}
default-The class is only accessible by classes in the same package. This
is used when you don’t specify a modifier.
c l a s s MyClass {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
50 / 85
Access Specifiers for attributes, methods and constructors
public-The code is accessible for all classes
p u b l i c c l a s s Main {
p u b l i c S t r i n g fname = ” John ” ;
p u b l i c S t r i n g lname = ”Doe ” ;
p u b l i c S t r i n g e m a i l = ” john@doe . com ” ;
p u b l i c i n t age = 2 4 ;
}
c l a s s Second1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( ” Name : ” + myObj . fname + ” ” +
System . o u t . p r i n t l n ( ” E m a i l : ” + myObj . e m a i l ) ;
System . o u t . p r i n t l n ( ” Age : ” + myObj . age ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
51 / 85
Access Specifiers for attributes, methods and constructors
private-The code is only accessible within the declared class
p u b l i c c l a s s Main {
p r i v a t e S t r i n g fname = ” John ” ;
p r i v a t e S t r i n g lname = ”Doe ” ;
p r i v a t e S t r i n g e m a i l = ” john@doe . com ” ;
i n t age = 2 4 ;
}
class test{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( ” Name : ” + myObj . fname + ” ” +
System . o u t . p r i n t l n ( ” E m a i l : ” + myObj . e m a i l ) ;
System . o u t . p r i n t l n ( ” Age : ” + myObj . age ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
52 / 85
Access Specifiers for attributes, methods and constructors

Protected-The code is accessible in the same package and subclasses.


c l a s s Person {
p r o t e c t e d S t r i n g fname = ” John ” ;
p r o t e c t e d S t r i n g lname = ”Doe ” ;
p r o t e c t e d S t r i n g e m a i l = ” john@doe . com ” ;
p r o t e c t e d i n t age = 2 4 ;
}
c l a s s Student extends Person {
p r i v a t e i n t graduationYear = 2018;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
S t u d e n t myObj = new S t u d e n t ( ) ;

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
53 / 85
Protected contd..

System . o u t . p r i n t l n ( ” Name : ” + myObj . fname + ” ” + myOb


System . o u t . p r i n t l n ( ” E m a i l : ” + myObj . e m a i l ) ;
System . o u t . p r i n t l n ( ” Age : ” + myObj . age ) ;
System . o u t . p r i n t l n ( ” G r a d u a t i o n Year : ” + myObj . g r a
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
54 / 85
default access specifier for attributes, methods and
constructors
default-The code is only accessible in the same package. This is used
when you don’t specify a modifier.
c l a s s Person {
S t r i n g fname = ” John ” ;
S t r i n g lname = ”Doe ” ;
S t r i n g e m a i l = ” john@doe . com ” ;
i n t age = 2 4 ;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
P e r s o n Ob = new P e r s o n ( ) ;
System . o u t . p r i n t l n ( ” Name : ” +Ob . fname +” ”+Ob . lnam
System . o u t . p r i n t l n ( ” E m a i l : ” +Ob . e m a i l ) ;
System . o u t . p r i n t l n ( ” Age : ” +Ob . age ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
55 / 85
this keyword

• The this keyword refers to the current object in a method or


constructor.
• Invoke current class constructor
• Invoke current class method
• Return the current class object
• Pass an argument in the method call
• Pass an argument in the constructor call
• The most common use of the this keyword is to eliminate the
confusion between class attributes and parameters with the same
name

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
56 / 85
Local and Instance Variables

• Local variables Variables defined inside methods, constructors or


blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed when
the method has completed.
• Instance variables Instance variables are variables within a class but
outside any method. These variables are initialized when the class is
instantiated. Instance variables can be accessed from inside any
method, constructor or blocks of that particular class.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
57 / 85
Variable Shadowing
If the instance variable and local variable have same name whenever you
print (access) it in the method. The value of the local variable will be
printed (shadowing the instance variable).
p u b l i c c l a s s FieldShadowingExample {
S t r i n g name = ” K r i s h n a ” ;
i n t age = 2 5 ;
public void d i s p l a y (){
S t r i n g name = ” V i s h n u ” ;
i n t age = 2 2 ;
System . o u t . p r i n t l n ( ” Name : ”+ t h i s . name ) ;
System . o u t . p r i n t l n ( ” age : ”+ t h i s . age ) ;
System . o u t . p r i n t l n ( ” Name : ”+name ) ;
System . o u t . p r i n t l n ( ” age : ”+age ) ;
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
58 / 85
Variable Shadowing

p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
F i e l d S h a d o w i n g E x a m p l e ob = new F i e l d S h a d o w i n g E x a m p l e ( )
ob . d i s p l a y ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
59 / 85
Encapsulation

• Encapsulation is defined as the wrapping up of data under a single


unit.
• It is the mechanism that binds together code and the data it
manipulates.
• Technically in encapsulation, the variables or data of a class is hidden
from any other class and can be accessed only through any member
function of its own class in which it is declared.
• Encapsulation can be achieved by Declaring all the variables in the
class as private and writing public methods in the class to set and get
the values of variables
• It is more defined with setter and getter method.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
60 / 85
Encapsulation

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
61 / 85
Example for Encapsulation

c l a s s Encapsulate {
p r i v a t e S t r i n g geekName ;
private int geekRoll ;
p r i v a t e i n t geekAge ;
p u b l i c i n t getAge ( )
{
r e t u r n geekAge ;
}
p u b l i c S t r i n g getName ( )
{
r e t u r n geekName ;
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
62 / 85
public int getRoll ()
{
return geekRoll ;
}
p u b l i c v o i d s e t A g e ( i n t newAge )
{
geekAge = newAge ;
}
p u b l i c v o i d setName ( S t r i n g newName )
{
geekName = newName ;
}
p u b l i c void s e t R o l l ( i n t newRoll )
{
geekRoll = newRoll ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
63 / 85
public class TestEncapsulation {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
E n c a p s u l a t e o b j = new E n c a p s u l a t e ( ) ;
o b j . setName ( ” Harsh ” ) ;
obj . setAge ( 1 9 ) ;
obj . s e t R o l l (51);
System . o u t . p r i n t l n ( ” Geek ’ s name : ” + o b j . getName ( ) ) ;
System . o u t . p r i n t l n ( ” Geek ’ s age : ” + o b j . getAge ( ) ) ;
System . o u t . p r i n t l n ( ” Geek ’ s r o l l : ” + o b j . g e t R o l l ( ) ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
64 / 85
Inheritance

• Inheritance is an important pillar of OOP(Object-Oriented


Programming).
• It is the mechanism in java by which one class is allowed to inherit
the features(fields and methods) of another class.
• Super Class: The class whose features are inherited is known as
superclass.
• Sub Class: The class that inherits the other class is known as a
subclass(or a derived class, extended class, or child class).
The subclass can add its own fields and methods in addition to the
superclass fields and methods.

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
65 / 85
Inheritance contd..

Reusability: Inheritance supports the concept of “reusability”, i.e. when


we want to create a new class and there is already a class that includes
some of the code that we want, we can derive our new class from the
existing class.
The keyword used for inheritance is extends.
c l a s s d e r i v e d −c l a s s e x t e n d s base −c l a s s
{
// methods and f i e l d s
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
66 / 85
Single Inheritance

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
67 / 85
Example for Single Inheritance

import java . io . ∗ ;
import java . lang . ∗ ;
import java . u t i l . ∗ ;
c l a s s one {
public void print geek ()
{
System . o u t . p r i n t l n ( ” S a i ” ) ;
}
}
c l a s s two e x t e n d s one {
public void p r i n t f o r () {
System . o u t . p r i n t l n ( ” f o r ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
68 / 85
Example for Single Inheritance

p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
two g = new two ( ) ;
g . print geek ();
g. print for ();
g . print geek ();
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
69 / 85
Multilevel Inheritance

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
70 / 85
Example for Multilevel Inheritance

import java . io . ∗ ;
import java . lang . ∗ ;
import java . u t i l . ∗ ;
c l a s s one {
public void print geek ()
{
System . o u t . p r i n t l n ( ” S u d h e e r ” ) ;
}
}
c l a s s two e x t e n d s one {
public void p r i n t f o r () {
System . o u t . p r i n t l n ( ” f o r ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
71 / 85
c l a s s t h r e e e x t e n d s two {
public void print geek ()
{
System . o u t . p r i n t l n ( ” S u n e e t h a ” ) ;
}
}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
t h r e e g = new t h r e e ( ) ;
g . print geek ();
g. print for ();
g . print geek ();
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
72 / 85
Hierarchical Inheritance

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
73 / 85
Example for Hierarchical inheritance

class A {
public void print A ()
{
System . o u t . p r i n t l n ( ” C l a s s A ” ) ;
}
}
c l a s s B extends A {
public void print B ()
{
System . o u t . p r i n t l n ( ” C l a s s B ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
74 / 85
c l a s s C extends A {
public void print C ()
{
System . o u t . p r i n t l n ( ” C l a s s C ” ) ;
}
}
c l a s s D extends A
{
public void print D ()
{ System . o u t . p r i n t l n ( ” C l a s s D ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
75 / 85
p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
B o b j B = new B ( ) ;
obj B . p r in t A ( ) ;
obj B . p r in t B ( ) ;
C o b j C = new C ( ) ;
obj C . print A ( ) ;
obj C . print C ( ) ;
D o b j D = new D ( ) ;
obj D . p r i n t A ( ) ;
obj D . p r i n t D ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
76 / 85
Super Keyword in Java

• The super keyword in java is a reference variable that is used to refer


parent class objects.
• Use of super with variables: This scenario occurs when a derived
class and base class has same data members. In that case there is a
possibility of ambiguity for the JVM.

class Vehicle
{
i n t maxSpeed = 1 2 0 ;
}
c l a s s Car e x t e n d s V e h i c l e
{
i n t maxSpeed = 1 8 0 ;

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
77 / 85
Super Contd...

void display ()
{
System . o u t . p r i n t l n ( ” Maximum Speed : ” + s u p e r . maxSpeed ) ;
}
}

c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
Car s m a l l = new Car ( ) ;
small . display ();
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
78 / 85
Super Contd..

Use of super with methods: This is used when we want to call parent
class method. So whenever a parent and child class have same named
methods then to resolve ambiguity we use super keyword.
c l a s s Person
{
v o i d message ( ) {
System . o u t . p r i n t l n ( ” T h i s i s p e r s o n c l a s s ” ) ;
}}
c l a s s Student extends Person {
v o i d message ( )
{
System . o u t . p r i n t l n ( ” T h i s i s s t u d e n t c l a s s ” ) ;
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
79 / 85
Super Contd..

void display ()
{
message ( ) ;
s u p e r . message ( ) ;
}
}
c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
S t u d e n t s = new S t u d e n t ( ) ;
s . display ();
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
80 / 85
Super contd..
Use of super with constructors: super keyword can also be used to
access the parent class constructor.
c l a s s Person
{
Person (){
System . o u t . p r i n t l n ( ” P e r s o n c l a s s C o n s t r u c t o r ” ) ;
}}
c l a s s Student extends Person
{
Student ()
{
super ( ) ;
System . o u t . p r i n t l n ( ” S t u d e n t c l a s s C o n s t r u c t o r ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
81 / 85
super contd..

c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S t u d e n t s = new S t u d e n t ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
82 / 85
final Keyword

final applied to variables


c l a s s Bike9 {
f i n a l i n t s p e e d l i m i t =90;// f i n a l v a r i a b l e
void run (){
s p e e d l i m i t =400;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
B i k e 9 o b j=new B i k e 9 ( ) ;
obj . run ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
83 / 85
final keyword in Java

final applied for method


c l a s s Bike {
f i n a l v o i d r u n ( ) { System . o u t . p r i n t l n ( ” r u n n i n g ” ) ; }
}

c l a s s Honda e x t e n d s B i k e {
v o i d r u n ( ) { System . o u t . p r i n t l n ( ” r u n n i n g s a f e l y w i t h

p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
Honda honda= new Honda ( ) ;
honda . r u n ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
84 / 85
final keyword in java

final applied for class


f i n a l c l a s s B i k e {}

c l a s s Honda1 e x t e n d s B i k e {
v o i d r u n ( ) { System . o u t . p r i n t l n ( ” r u n n i n g s a f e l y w i t h 1

p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
Honda1 honda= new Honda1 ( ) ;
honda . r u n ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
85 / 85

You might also like