Explain dynamic method dispatch with example.
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Therefore, it is the mechanism of achieving run time polymorphism. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred by a superclass reference variable, different versions of the method are executed.
Here is an example that illustrates dynamic method dispatch:
class A
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class B extends A
void callme()
{
System.out.println("Inside B's callme method");
}
}
class C extends B
{
void callme()
{
System.out.println("Inside C's callme method");
}
}
class Dispatch
{
public static void main(String[] args)
{
A a= new A();
B b = new B();
C c= new C();
A r;
r = a;
r.callme();
r = b;
r.callme();
r = c;
r.callme();
}
}
Output:
Inside A's callme method
Inside B's callme method
Inside C's callme method
OR,
- Dynamic method dispatch is the mechanism in which a call to an overridden method is resolved at run time instead of compile time. This is an important concept because of how Java implements run-time polymorphism.
- Java uses the principle of ‘a superclass reference variable can refer to a subclass object’ to resolve calls to overridden methods at run 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.
- In other words, it is the type of object being referred to that determines which version of an overridden method will be executed.
Advantages of dynamic method dispatch
- It allows Java to support overriding of methods, which are important for run-time polymorphism.
- It allows a class to define methods that will be shared by all its derived classes, while also allowing these sub-classes to define their specific implementation of a few or all of those methods.
- It allows subclasses to incorporate their own methods and define their implementation.
// Implementing Dynamic Method Dispatch
class Apple
{
void display()
{
System.out.printIn("Inside Apple's display method");
}
}
class Banana extends Apple
{
void display() // overriding display()
{
System.out.printIn("Inside Banana's display method");
}
}
class Cherry extends Apple
{
void display() // overriding display()
{
System.out.printIn("Inside Cherry's display method");
}
}
class Fruits_Dispatch
{
public static void main(String args[])
{
Apple a = new Apple(); // object of Apple
Banana b = new Banana(); // object of Banana
Cherry c = new Cherry(); // object of Cherry
Apple ref; // taking a reference of Apple
ref = a; // r refers to a object in Apple
ref.display(); // calling Apple's version of display()
ref = b; // r refers to a object in Banana
ref.display(); // calling Banana's version of display()
ref = c; // r refers to a object in Cherry
ref.display(); // calling Cherry's version of display()
}
}
Comments
Post a Comment