In java, base class reference can be assigned objects of sub class. When methods of sub class object are called through base class’s reference.
The mapping / binding or function calls to respective function takes place after running the program, at least possible moment. This kind of binding is known as “late binding” or “dynamic binding” or “runtime polymorphism”.
Eg.
class A
{
public void display()
{ }
}
class B extends A
{
public void display()
{ }
}
class C extends A
{
public void display()
{ }
}
class DispatchDemo
{
psvm (String args[])
{
A ob1= new A();
B ob2 = new B();
C ob3 = new C();
A r;
r=ob1;
r.display();
r=ob2;
r.display();
r=ob3;
r.display();
}
}
In the above programs, the statement r.display( ), calls the display() method and based on the object. That has been assigned to base class reference. i.e. r. But the mapping / binding as to which method is to be invoked is done only at run time.