KEMBAR78
Unit 4 | PDF | Programming | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
8 views36 pages

Unit 4

Uploaded by

pp8855066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views36 pages

Unit 4

Uploaded by

pp8855066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit-4

Object Oriented
Programming Concepts
Contents
• Object Reference
• Constructor & Constructor Overloading
• Method & Method Overloading
• Passing and Returning an Object from Method
• ‘new’ operator
• ‘this’ keyword
• ‘static’ keyword
• Garbage Collection and finalize() Method
• Nested Class and Inner Class
• Anonymous Inner Class
• Abstract Class
Object Reference
• Its purpose is to refer or point the actual object.
• The variable stores information about where the object is located.
• Object variables in Java are sometimes referred to as object reference variables.
• In Java, a reference variable is a variable that holds the memory address of an object rather than the
actual object itself.
• When an object is created using the new keyword, memory is allocated on the heap to store the
object's data.
• The reference variable is then used to refer to this memory location, making it possible to access
and manipulate the object's properties and behaviors.
Object Reference
• Example:
class Car {
String brand;
}
public class Demo {
public static void main(String[] args) {
Car obj; //A reference variable of type Car
obj = new Car(); Output:
obj.brand = “Toyota”; Brand: Toyota

System.out.println("Brand: " + obj.brand);


}
}
Constructor
• A constructor is a block of codes similar to the method.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, at least one constructor is called.
• It calls a default constructor if there is no constructor available in the class.
• Rules to create constructors:
1. Its name must be the same as its class name.
2. It does not have any return type.
3. It cannot be abstract, static, final, and synchronized.
• Types of Constructors:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
• A constructor is called "Default Constructor" when it doesn't have any parameter.
• Syntax:
class_name() { … }
• Example:
class A {
A() {
System.out.println(“It’s a Default Constructor.”);
}
public static void main(String args[]) { Output:
A obj = new A(); //calling a default constructor It’s a Default Constructor.
}
}
Parameterized Constructor
• A constructor which has a specific number of parameters is called a parameterized constructor.
• Syntax:
class_name( type1 variable1, <type2 variable2, ….> ) { … }
• Example:
class A {
A(int a) {
System.out.println(“Value of parameter a = ” + a);
} Output:
public static void main(String args[]) { Value of parameter a = 10
A obj = new A(10); //calling a parameterized constructor
}
}
Copy Constructor
• By using Copy Constructor, we can copy the values from one object to another.
• Syntax:
class_name( ClassName object) { … }
• Example:
class A {
int a;
A() { a = 10; }
A(A obj) {
a = obj.a;
}
void display() {
System.out.println(“a = ”+a);
}
}
Copy Constructor
class Demo {
public static void main(String args[]) {
A obj1 = new A();
A obj2 = new A(obj1); //calling a copy constructor
obj1.display();
obj2.display();
}
}

• Output:
a = 10
a = 10
Constructor Overloading
• Constructor overloading is a technique of having more than one constructors with different
parameter lists.
• They are arranged in a way that each constructor performs a different task.
• They are differentiated by the compiler by the number of parameters in the list and their types.

• Syntax:
ClassName() {
}
ClassName(type1 varible1 <, type2 variable2, …>){
}

Constructor Overloading
• Example:
class A {
int a;
A() {
a = 10;
}
A(int b) {
a = b;
}
void display() {
System.out.println(“a = ”+a);
}
}
Constructor Overloading
class Demo {
public static void main(String args[]) {
A obj1 = new A();
A obj2 = new A(20);
obj1.display();
obj2.display();
}
}

• Output:
a = 10
a = 20
Method
• A method is a way to perform some task.
• A method is a collection of instructions or a block of statements that performs a specific task.
• It provides the reusability of code.
• A method consists of Access Specifier, Return type, Method name, Parameter list and Method
body.
• Types of Method:
1. Predefined Method: It is called a standard library method or built-in method.
2. User-defined Method: It is created by the user’s choice.
• Example:
public int sum (int a, int b) {
//Method Body;
}
Method
• Method Signature: Every method has a method signature.
• It is a part of the method declaration.
• It includes the method name and parameter list.
Method Overloading
• If a class has multiple methods having same name but different parameters, then it is known as
Method Overloading.
• Example:
public class A {
public void show() {
System.out.println(“Method 1”);
}
public void show(int a) { //This is method overloading
System.out.println(“Method 2”);
System.out.println(“Parameter a = ”+a);
}
}
Method Overloading
public class Demo {
public static void main (String args[]){
A obj = new A();
obj.show();
obj.show(10);
}
}

• Output:
Method 1
Method 2
Paremeter a = 10
Recursion
• It is a process where a method calls itself again and again.
• Syntax:
returntype methodname(){
//code to be executed
methodname(); //calling same method
}
• Example:
void print(){
System.out.println(“Hello");
print();
}
Passing an Object from Method
• Although Java is strictly passed by value, we can pass objects as parameters to the methods in java.
• Objects are passed by call-by-reference mechanism.
• Example:
class A {
static void print(A obj) {
System.out.println(“Object passed to this method.”);
}
public static void main(String args[]) {
A a1 = new A();
print(a1);
}
}
• Output: Object passed to this method.
Returning an Object from Method
• Example:
class A {
A copyObj() {
System.out.println(“Returning an object from a method.”);
A obj = new A();
return obj;
}
public static void main(String args[]) {
A a1 = new A();
A a2 = a1. copyObj();
}
}
• Output: Returning an object from a method.”);
‘new’ operator
• It is used to create an instance of a class, also known as an object.
• It is used to allocate memory for the object on the heap, the memory space where objects are
stored.
• It calls the constructor of a class to initialize the object.
• Syntax:
ClassName object = new ClassName();
• Example:
A obj = new A();
‘this’ keyword
• It is used to refer the current object.
• It can be used to invoke current class method and current class constructor.
• Example:
class Demo {
int a;
Demo(int a) {
this.a = a;
}
public static void main(String args[]) { Output:
Demo d1 = new Demo(10); a = 10
System.out.println(“a = ”+d1.a);
}
}
‘static’ keyword
• Static keyword in java indicates that a particular member is not an instance, but rather a part of a
Class.
• The static member will be shared among all instances of the class, so only one instance of it will be
created.
• Static members can be accessed before the class is initiated.
• Static member will be accessed by using ‘ClassName.MemberName’ mechanism.
• Static keyword can be used with:
1. Variable
2. Method
3. Block
Static Variable
• The static variable can be used to refer to the common property of all objects (which is not unique for
each object), for example, the company name of employees, college name of students, etc.
• The static variable gets memory only once in the class area at the time of class loading.
• Example:
class Student {
static String college = "Gandhinagar University";
}
class Test {
public static void main(String args[]) {
System.out.println(Student.college);
}
}
• Output: Gandhinagar University
Static Method
• A static method belongs to the class rather than the object of a class.
• A static method can access static data member and can change the value of it.
• Example:
class A {
static void show() {
System.out.println(“Static method invoked.”);
}
}
class Test {
public static void main(String args[]) {
A.show();
}
}
• Output: Static method invoked.
Static Block
• It is used to initialize the static data members.
• It is executed before the main method at the time of class loading.
• Example:
class A {
static {
System.out.println("Static block invoked.");
}
Output:
public static void main(String args[]) { Static block invoked.
System.out.println("Main method invoked."); Main method invoked.

}
}
Garbage Collection
• In java, garbage means unreferenced objects.
• Garbage Collection is a process of reclaiming the runtime unused memory automatically.
• In other words, it is a way to destroy the unused objects.
• To do so, we were using free() function in C language and delete() in C++.
• But, in java it is performed automatically.
• So, java provides better memory management.
• It makes java memory efficient.
• It is automatically done by the garbage collector (a part of JVM).
• Any object can be unreferenced by nulling the reference, by assigning a reference to another and
by anonymous object etc.
finalize() Method
• The finalize() method is invoked each time before the object is garbage collected.
• This method can be used to perform cleanup processing.
• This method is defined in Object class.
• Syntax:
protected void finalize(){ }

➢ gc() method: The gc() method is used to invoke the garbage collector to perform cleanup
processing.
• The gc() is found in System and Runtime classes.
• Syntax:
public static void gc(){ }
Nested Class and Inner Class
Nested Class and Inner Class
• Java inner class or nested class is a class that is declared inside the class or interface.
• It can access all the members of the outer class, including private data members and methods.
• It supports Code Optimization which requires less code to write.
• Static nested class is called Nested class.
• Non-static nested class is called Inner class.
• Non-static inner class includes:
1. Member inner class
2. Anonymous inner class
3. Local inner class
Nested Class: Example
class Outer {
int a = 10;
static class Nested {
void show() {
System.out.println(“a = ”+a);
}
}
public static void main(String args[]) {
Outer.Nested obj = new Outer.Nested(); Output:
obj.show(); a = 10
}
}
Inner Class: Example
class Outer {
int a = 10;
class Inner {
void show() {
System.out.println(“a = ”+a);
}
}
public static void main(String args[]) {
Outer obj = new Outer();
Output:
Outer.Inner inObj = obj.new Inner();
a = 10
inObj.show();
}
}
Anonymous Inner Class
• Java anonymous inner class is an inner class without a name.
• For Anonymous Inner Class, only a single object is created.
• It should be used to override a method of class or interface.

• Java Anonymous inner class can be created in two ways:


1. Abstract Class
2. Interface
Anonymous Inner Class
1. Using Abstract Class: Example
abstract class A{
abstract void show();
}
class Test {
public static void main(String args[]) {
A obj = new A(){
void show(){
System.out.println(“Anonymous Inner Class.");
}
};
Output:
obj.show();
Anonymous Inner Class.
}
}
Anonymous Inner Class
2. Using Interface: Example
interface A{
void show();
}
class Test {
public static void main(String args[]) {
A obj = new A(){
public void show(){
System.out.println(“Anonymous Inner Class.");
}
};
Output:
obj.show();
Anonymous Inner Class.
}
}
Abstract Class
• A class which is declared with the abstract keyword is known as an abstract class in Java.
• It can have abstract and non-abstract methods.
• It can have constructors and static, non-static methods also.
• Abstract methods means methods without body.
• It needs to be extended and its method implemented.
• It cannot be instantiated means its objects can not created.
• It supports OOP concept called Abstraction.
• It uses ‘extends’ keyword.
• Syntax:
abstract class ClassName {

}
Abstract Class: Example
abstract class Bike {
abstract void run();
}
class Honda extends Bike {
void run() {
System.out.println(“Running…");
}
public static void main(String args[]){
Bike obj = new Honda(); Output:
Running...
obj.run();
}
}

You might also like