Runtime polymorphism and dynamic binding

Runtime polymorphism and dynamic binding:

Resolving a method call that is finding out which method definition is to be executed by a method call is called binding.
Binding is down by the compiler. In normal cases compiler identify a method definition for a method call at compilation time that is method call is resolved at compilation time. Resolving a method call at compilation time is static binding. In case of method overloading static binding is performing.
“Reference variable of a base class can contain the references of its sub class objects.”
Class A{
Public void display()
{
s.out.println(“display of A”);
}
}
Class B extends A{
Public void display(){
s.out.println(“display of B”);
}
}
Class c extends A
{
Public void display(){
s.out.println(“display of C”);
}
}
Class D
{
Public static void main(String as[]){
A x=new A();
B y=new B();
C z=new Z();
callMe(x);
callMe(y);
callMe(z);
}
Private static callMe(A p){
p.display();
}
}
“If a method call to an overridden method is made using the reference variable of the base class then compiler does not bind method call to a method at compilation time, rather it postponed the binding of the method call up to the execution time. When the type of object being referenced by the reference variable becomes known. Such type of method binding is called dynamic or late binding and it facilited run time polymorphism.”
Runtime polymorphism is the facility to invoke different method from a single method calls.
“To create generalize method use run time polymorphism.”
“When a reference variable of a base class is used to refer sub class object then only members are referred which are part of base class.”

No comments:

Post a Comment