Dynamic Method Dispatch

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Dynamic Method Despatch

Dynamic method dispatch is the mechanism in which a call to an


overridden method is resolved at run time instead of compile
time. ... When a superclass reference is used to call an overridden
method, Java determines which version of the method to execute
based on the type of the object being referred to at the time call.
package pack1;

class A
{
public void display( )
{
System.out.println("Class A Display Method");
}
}
class B extends A
{
public void display( )
{
System.out.println("Class B Display Method");
}
}
class C extends B
{
public void display( )
{
System.out.println("Class C Display Method");
}
}
public class OverridingDemo
{
public static void main(String[] args)
{
A obj;
obj = new C( );
obj.display( );

obj = new B( );
obj.display( );

obj = new A( );
obj.display( );
C obj1 = new C();
obj1.display( );

You might also like