0% found this document useful (0 votes)
30 views21 pages

Unit 3 - Lecture-6

The document discusses object-oriented programming concepts like inheritance, polymorphism, method overloading, overriding, and passing objects in Java. It provides examples of inheritance with constructors and accessing superclass members. Method overloading examples show resolving calls at compile-time, while overriding examples show dynamic binding at runtime. Private and final methods cannot be overridden. Passing objects to methods actually passes the object's memory location, so changes within methods affect the original object.

Uploaded by

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

Unit 3 - Lecture-6

The document discusses object-oriented programming concepts like inheritance, polymorphism, method overloading, overriding, and passing objects in Java. It provides examples of inheritance with constructors and accessing superclass members. Method overloading examples show resolving calls at compile-time, while overriding examples show dynamic binding at runtime. Private and final methods cannot be overridden. Passing objects to methods actually passes the object's memory location, so changes within methods affect the original object.

Uploaded by

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

Object Oriented Programming

Prof. Rahul B. Diwate

10/9/2021 1
Unit-3
• Inheritance:
• Inheritance in Java,
• Types,
• Constructor in Inheritance,
• Using final with Inheritance,
• Accessing superclass member,
• Override private methods,
• Parent and Child classes having same data member,
• Base vs derived class reference.
• Polymorphism:
• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding,
• Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
Polymorphism

• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding, Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
Polymorphism

Compile time (Static/Early) Run time (Dynamic/Late)


binding binding

Method Overriding
Overloading
Polymorphism: Static and Dynamic
• Static polymorphism (or Early Binding) is a type of polymorphism that
resolves at compile time. Method overloading is an example of static
polymorphism. In method overloading, there are methods with the same
name but different parameters. In other words, there are methods with the
same name, but they have different data types and a different number of
arguments. Moreover, the method to call is determined at compile time.
An example program is as follows.
Example:
• There are two methods in the test class with the same name “add”.
• The first add method accepts two integers and returns the sum of those two numbers.
• Similarly, the second add method accepts three integers and return the sum of those
three numbers.
• In the main method, there is an object of the type Test class. Then, the add methods are
called on the object. Line 12 calls the add method with two parameters while Line 13 calls
add method with three parameters.
• Likewise, the method to call is determined at the compile time.
Example: Class Overloading
class Adder
{
static int add(int a, int b)
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
} }
class Overloading
{
public static void main(String[] args)
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
} }
Example: Constructor Overloading
public class Class_Overloading {
//instance variables of the class
int id;
String name;
Class_Overloading(){
System.out.println("this a default constructor");
}
Class_Overloading(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
//object creation
Class_Overloading s = new Class_Overloading();
System.out.println("\nDefault Constructor values: \n");
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);
System.out.println("\nParameterized Constructor values: \n");
Class_Overloading student = new Class_Overloading(101, "VITEDU");
System.out.println("Student Id : "+student.id + "\nStudent Name :
"+student.name);
} }
class Maximum
Example: Method Overloading
{
public int max(int a, int b)
{
return a>b?a:b;
}
public int max(int a,int b,int c)
{
if(a>b && a>c) return a;
else if(b>c) return b;
return c;
} }
public class Method {
public static void main(String[] args)
{
Maximum t=new Maximum();
//t.max(13, 5);
System.out.println("the maximum is " + t.max(10,50));
System.out.println("the maximum is " + t.max(100,150,50));
}
}
What is Dynamic Polymorphism in Java
• Dynamic Polymorphism (or Late Binding) is a type of polymorphism that resolves at run time. Method
overriding is an example of dynamic polymorphism. In method overriding, there are two classes: one is
the parent class while the other is the child class. However, both classes have the same method.
Moreover, the method in the child class overrides the method of the parent class.
• An example is as follows
Class A has a display method and Class B also has a display method. Moreover, Class B extends class A. In
the main method, a child class object is assigned to the parent class reference.
It determines which method to call at run time. Then, the display method is called using the object. The
programmer can see that the display method of class B executes.
In other words, the display method of class A is overridden by the display method of class B.
class Parent { Example: Method Overriding
void Print()
{
System.out.println("parent class is printed");
} }
class subclass1 extends Parent {
void Print()
{ System.out.println("subclass1 is
printed");
}}
class subclass2 extends Parent {
void Print()
{ System.out.println("subclass2");
} }
class Method_Overriding {
public static void main(String[] args)
{
Parent a;
a = new Parent();
a.Print();
// a = new subclass2();
//a.Print();
}}
class Super Example: Class Overriding
{
public void display()
{
System.out.println("Super display Method Called");
}}
class Sub extends Super
{
public void display()
{
System.out.println("Sub display Method Called");
}}
public class Class_Overriding
{
public static void main(String[] args)
{
Super s=new Sub();
s.display();
} }
class Parent1 { Example: SubClass Overriding
void Print()
{
System.out.println("parent class");
}}
class subclass11 extends Parent1 {
void Print()
{ System.out.println("subclass1");
} }
class subclass22 extends Parent1 {
void Print()
{ System.out.println("subclass2");
}}
class SubClass_Overriding {
public static void main(String[] args)
{
Parent1 a;
a = new subclass11();
a.Print();
a = new subclass22();
a.Print();
} }
private and final methods in polymorphism
• In Java private methods are the methods having private access modifier and are restricted to be
access in the defining class only and are not visible in their child class due to which are not eligible
for overridden.
• However, we can define a method with the same name in the child class and could access in parent
class.
• Like private methods final methods in Java are the methods having final non-access modifier instead
of private and are again restricted to be accessed in the defining class only and are not visible in their
child class due to which are not eligible for overridden.
• The only difference between private and final methods is that in case of final methods we even can't
define a method with the same name in child class while in case of private methods we could define.
• In Java as both private and final methods do not allow the overridden functionality so no use of using
both modifiers together with same method.
public class PrivateFinalMethod {
private void print() {
System.out.println("in parent print");
}
public static void main(String[] args) {

PrivateFinalMethod obj = new PrivateFinalMethodsChild();


obj.print();
PrivateFinalMethodsChild obj1 = new PrivateFinalMethodsChild();
obj1.print();
}
}
class PrivateFinalMethodsChild extends PrivateFinalMethod {
public void print(){
System.out.println("in child print method");
}
}
Passing and Returning Objects in Java
• Reference in Java holds the memory location of object created if it is assigned to
that reference otherwise it is initiated as null.
• Now the point to remember here is that the value of the reference is the memory
location of the assigned object, so whenever we pass the reference to any method
as argument then we actually pass the memory location of that object which is
assigned to that particular reference.
• This technically means that the target method has memory location of our created
object and can access it to.
• So in case if target method access our object and make changes to any of property
of it than we would encounter with the changed value in our original object.
public class PassByValue {
static int k =10;
static void passPrimitive(int j) {
System.out.println("the value of passed primitive is " + j);
j = j + 1;
}
static void passReference(EmployeeTest emp) {
EmployeeTest reference = emp;
System.out.println("the value of name property of our object is "+ emp.getName());
reference.setName("Bond");
}
public static void main(String[] args) {
EmployeeTest ref = new EmployeeTest();
ref.setName("James");
passPrimitive(k);
System.out.println("Value of primitive after get passed to method is "+ k);
passReference(ref);
System.out.println("Value of property of object after reference get passed to method is "+ ref.getName());
} }
class EmployeeTest {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
class Operation{
int data=50;

void changeVar(int data){


// Changes will be in the local variable only
data=data+100;
}

void changeObj(int data){


//Changes will be in the instance variable
this.data=this.data+data;
}

public static void main(String args[]){


Operation op=new Operation();

op.changeVar(500);
System.out.println("after change "+op.data);
op.changeObj(500);
}
}
// Java program to demonstrate objects passing to methods.
class ObjectPassDemo
{
int a, b;

ObjectPassDemo(int i, int j)
{
a = i;
b = j;
}

// return true if o is equal to the invoking


// object notice an object is passed as an
// argument to method
boolean equalTo(ObjectPassDemo o)
{
return (o.a == a && o.b == b);
}
}

// Driver class
public class Test
{
public static void main(String args[])
{
ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);
ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);

System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));


System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));
}

You might also like