Unit 3 - Lecture-6
Unit 3 - Lecture-6
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
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) {
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;
}
// 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);