Runtime Polymorphism in Java
Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime.
In runtime polymorphism , an overridden method is called through the reference variable of parent class or super class.
Upcasting in Java
The reference id of child class object can be put into the reference variable of parent class, this concept is known as upcasting in java.
For example:
In the below example, We create two class Animal and Dog, Dog extends Animal class and overrides run() method. We are calling the run() method by the reference variable of parent class. Since it refers to the object of child class and child class method overrides the parent class method, child class method is invoked at runtime. Here method invocation is determined by the JVM not compiler. So it is known as runtime polymorphism.
class Animal
{
void run()
{
System.out.println("all animals runs slow or fast");
}
}
class Dog extends Animal
{
void run()
{
System.out.println("dog run fast");
}
public static void main(String args[])
{
Animal a = new Dog();//upcasting
a.run();
}
}
output: dog run fast
For example:
In the below example, We create two class Animal and Dog, Dog extends Animal class and overrides run() method. We are calling the run() method by the reference variable of parent class. Since it refers to the object of child class and child class method overrides the parent class method, child class method is invoked at runtime. Here method invocation is determined by the JVM not compiler. So it is known as runtime polymorphism.
class Animal
{
void run()
{
System.out.println("all animals runs slow or fast");
}
}
class Dog extends Animal
{
void run()
{
System.out.println("dog run fast");
}
public static void main(String args[])
{
Animal a = new Dog();//upcasting
a.run();
}
}
output: dog run fast
Java Runtime Polymorphism with Multilevel Inheritance
For example:
class GrandFather
{
void relation()
{
System.out.println("Grand Father");
}
}
class Father extends GrandFather
{
void relation()
{
System.out.println("Son of GrandFather");
}
}
class Son extends Father
{
void relation()
{
System.out.println("Son of Father");
}
}
class Test
{
public static void main(String args[])
{
GrandFather g = new Father();
g.relation();
Father f = new Son();
f.relation();
}
}
output: Son of GrandFather
Son of Father
You can't access of personal method of child class...
Whenever you do up-casting then you cannot access the personal method of child class by the reference variable of parent class.
For example:
class Parent
{
void show()
{
System.out.println("Parent");
}
}
class Child extends Parent
{
void personalMethod()//can't access by reference variable of parent
{
System.out.println("personal method of child class");
}
void show()
{
System.out.println("child");
}
public static void main(String args[])
{
Parent p = new Child();
p.show();
p.personalMethod();//compile time error
}
}
output: compile time error
Runtime Polymorphism with Data Member
you cannot achieve runtime polymorphism with data members.
In the example given below there are two class with same data member i.e age. We are accessing the data member by the reference variable of parent class which refer to the child class object. Since we are accessing the data member which is not overridden , hence it will access the data member of parent class always.
For example:
class Company
{
int age = 60;
}
class Employee extends Company
{
int age = 30;
public static void main(String args[])
{
Company c = new Employee();
System.out.println(c.age);
}
}
output: 60
You can't put the reference Id of Parent class object into child class reference variable
You cannot put the reference id of parent class object into child class reference variable.
For example:
class Parent
{
void show()
{
System.out.println("parent");
}
}
class Child extends Parent
{
void show()
{
System.out.println("child");
}
public static void main(String args[])
{
Child c =new Parent();//compile time error
Child c =new Parent();//compile time error
c.show();
}
}
output: compile time error
0 comments:
Post a Comment