Saturday, February 28, 2015

What is Runtime Polymorphism in JAVA?

Runtime polymorphism, often referred to as Dynamic Binding in OOP (Object Oriented Paradigm) concepts of Java where the method to be invoked is decided at the runtime.
This is a case of Overriding where a method say display() is overridden in the child class, and this method is called by an object variable of Parent class containing the reference of the Child class.

Eg :
class Base{
public Base(){}
public void display(){
System.out.println("Base");
}
}

class Child extends Base{
public Child(){}
public void display(){
System.out.println("Child");
}
}

class Test{
public static void main(String args[]){
Child obj = new Base();
obj.display();
}
}

Output of above program would be ==> Child

This is the case of dynamic binding where you see that when the display() is called from the base class object having reference of child class is called, child class method is called.
This is decided by JVM during the runtime.


No comments:

Post a Comment