OOP Chapter II
OOP Chapter II
School of Informatics
Department of Computer Science
Object 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.
Class
7
Object is an instance of a class. For example, suppose Bicycle is a class then sportsBicycle,
touringBicycle, etc can be considered as objects of the class.
Creating an Object using constructor
Here is how we can create an object of a class.
className object = new className();
// for Bicycle class
Bicycle sportsBicycle = new Bicycle();
Bicycle touringBicycle = new Bicycle();
We have used the new keyword along with the constructor of the class to create an object
sportsBicycle and touringBicycle.
Constructors are similar to methods and have the same name as the class. For
example, Bicycle() is the constructor of the Bicycle class.
10
we have created a Student class which has two data members id and name.
We are creating the object of the Student class by new keyword and printing
the object's value. Here, we are creating a main() method inside the class.
//Defining a Student class. File: Student.java
class Student{
//defining fields
int id;//field or data member or instance variable //Printing values of the object
String name; System.out.println(s1.id);//accessing member
//creating main method inside the Student class through reference variable
public static void main(String args[]){ System.out.println(s1.name);
//Creating an object or instance }
output
Student s1=new Student();//creating an object }
0
of Student null
12
We can have multiple classes in different Java files or single Java file.
If you define multiple classes in a single Java source file, it is a good idea to save the file
name with the class name which has main() method.
13 //Java Program to demonstrate having the main method in another class
//Creating Student class. File: TestStudent1.java
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
output
public static void main(String args[]){
0
Student s1=new Student();
null
System.out.println(s1.id);
System.out.println(s1.name);
}
}
14
Initializing an object means storing data into the object. Let's see a simple example where
we are going to initialize the object through a reference variable. //Save as TestStudent2.java
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=132; output
s1.name="Samuel"; 132 Samuel
//printing members with a white space
System.out.println(s1.id+" "+s1.name);
} }
We can also create multiple objects and store information in it through reference variable.
class Student{ // File: TestStudent3.java
16 int id;
String name; }
class TestStudent3 {
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=150;
s1.name="Mengistu";
s2.id=155; output
s2.name="Zewudineh"; 150 Mengistu
//Printing data 155 Zewudineh
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
} }
2) Object and Class Example: Initialization through method
17
There are two types of methods in Java: predefined and user defined.
In Java, predefined methods are the method that is already defined in the Java class libraries.
It is also known as the standard library method or built-in method. We can directly use
these methods just by calling them in the program at any point.
Some pre-defined methods are length(), equals(), compareTo(), sqrt(), max() etc.
The method written by the user or programmer is known as a user-defined method. These
methods are modified according to the requirement.
Example
public class Demo {
public static void main(String[] args) {
// using the max() method of Math class
System.out.print("The maximum number is: " + Math.max(20,10));
}
}
Method declaration
19
The method declaration provides information about method attributes, such as visibility, return-
type, name, and arguments.
It has four components that are known as method header, as we have shown in the following
figure.
20
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.
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.
21
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.
2) Object and Class Example: Initialization through method
22
Now we are creating the two objects of Student class and initializing the value to these objects
by invoking the user defined insertRecord, method.
Here, we are displaying the state (data) of the objects by invoking the displayInfo() method.
class Student{ //File: TestStudent4.java
class TestStudent4{
int rollno;
public static void main(String args[]){
String name;
Student s1=new Student();
void insertRecord(int r, string n){
Student s2=new Student();
rollno=r;
s1.insertRecord(102,“Meron");
name=n;
s2.insertRecord(202,“Bekele");
}
s1.displayInformation();
void displayInfo(){
s2.displayInformation();
System.out.println(rollno+" "+name);
}
} output
}
} 102 Meron
202 Bekele
3) Object and Class Example: Initialization through a constructor
23