by Dinesh Thakur Category: Classes

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.

 

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article