OOP Chapter 3
OOP Chapter 3
Chapter Three
Objects and Classes
3.1 . object and classes in Java
Object: an entity that has state and behavior. e.g., chair, bike, marker,
pen, table, car, etc. It can be physical or logical (tangible and intangible).
The example of an intangible object is the banking system.
An object has three characteristics:
➢ State: represents the data (value) of an object.
➢ Behavior: represents the behavior (functionality) of an object
such as deposit, withdraw, etc.
➢ Identity: An object identity is typically implemented via a
unique ID. The value of the ID is not visible to the external user. However, it is used internally by the
JVM to identify each object uniquely.
Example: Pen is an object. Its name is Bic; color is Blue, known as its state. It is used to write, so writing is its
behavior.
An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an
object is the instance(result) of a class.
Object Definitions:
➢ An object is a real-world entity.
➢ An object is a runtime entity.
➢ The object is an entity which has state and behavior.
➢ The object is an instance of a class.
class in Java
A class is a group of objects which have common properties. It is a template or blueprint from which objects
are created. It is a logical entity. It can't be physical.
A class in Java can contain:
➢ Fields
➢ Methods
➢ Constructors
➢ Blocks
1|Page
Object Oriented Programming in Java Lecture note
2|Page
Object Oriented Programming in Java Lecture note
Accessing Objects
Newly created objects are allocated in the memory. They can be accessed via reference variables (objects).
The objects of the classes are independent. A class is essentially a programmer-defined type. A class is
a reference type, which means that a variable of the class type can reference an instance of the class.
After an object is created, its data can be accessed and its methods invoked using the dot operator (.), also
known as the object member access operator.
Accessing a Data Fields
Syntax: (it is by reference of object).
objectRefVar.dataField;
Example:
circle1.radius; //References the radius in circle1.
Accessing a Methods
Syntax: (it is by reference of object).
objectRefVar.method(arguments);
Example:
circle1.getArea(); //Invokes the getArea method on circle1. Methods are invoked as operations on objects.
3|Page
Object Oriented Programming in Java Lecture note
B. Parameterized Constructors
The parameterized constructors can accept values into the constructor that has different parameters in the
constructor. The value would be transferred from the main ( ) method to the constructor either with direct
values or through variables. Parameterized Constructor has parameter list to receive arguments for
populating the instance attributes.
When you create a new instance (a new object) of a class using the new keyword, a constructor for that class
is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are
similar to methods, but with some important differences.
• A constructor must have the same name as the class itself.
• Constructors do not have a return type – not even void.
• Constructors are invoked using the new operator when an object is created. Constructors play the role
of initializing objects.
• Default constructor. If you don't define a constructor for a class, a default (parameter less)
constructor is automatically created by the compiler. The default constructor calls the default parent
constructor (super( ) and initializes all instance variables to default value (zero for numeric types, null
for object references, and false for Booleans).
4|Page
Object Oriented Programming in Java Lecture note
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
}
}
7|Page
Object Oriented Programming in Java Lecture note
Java Methods
A Java method is a collection of statements that are grouped together to perform an operation. When you call
the System.out.println method, for example, the system actually executes several statements in order to
display a message on the console. Now you will learn how to create your own methods with or without return
values, invoke a method with or without parameters, overload methods using the same names, and apply
method abstraction in the program design.
Method Signature: Every method has a method signature. It is a part of the method declaration. It includes
the method name and parameter list.
Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of
the method. Java provides four types of access specifier:
Public: The method is accessible by all classes when we use public specifier in our application.
Private: When we use a private access specifier, the method is accessible only in the classes in which
it is defined.
Protected: When we use protected access specifier, the method is accessible within the same package
or subclasses in a different package.
8|Page
Object Oriented Programming in Java Lecture note
Default: When we do not use any access specifier in the method declaration, Java uses default access
specifier by default. It is visible only from the same package only.
Return Type: Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to
the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the
method name must be subtraction(). A method is invoked by its name.
Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method has no parameter, left the parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed
within the pair of curly braces.
The main advantage of a static method is that we can call it without creating an object. It can access static data
members and also change the value of it. It is used to create an instance method. It is invoked by using the
class name. We can call a static method by using the ClassName.methodName.
The best example of a static method is the main() method. It is called without creating the object.
➢ A static method belongs to the class rather than the object of a class.
➢ A static method can be invoked without the need for creating an instance of a class.
9|Page
Object Oriented Programming in Java Lecture note
➢ A static method can access static data member and can change the value of it.
Example:
class A{
private A(){}//private constructor
void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();//Compile Time Error
} }
12 | P a g e
Object Oriented Programming in Java Lecture note
//saved by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Can be Accessed
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
13 | P a g e
Object Oriented Programming in Java Lecture note
❖ If the class is defined inside a package, then the package statement should be the first statement in the
source file.
❖ If import statements are present then they must be written between the package statement and the class
declaration.
❖ If there are no package statements then the import statement should be the first line in the source file.
Import and package statements will imply to all the classes present in the source file. It is not possible to
declare different import and/or package statements to different classes in the source file.
14 | P a g e