What is dynamic method dispatch in Java?
Published: 2021-04-23 10:56:17

**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
1.It allows Java to support `overriding of methods`, which are 
  important for run-time polymorphism.
2.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.
3.It allows subclasses to incorporate their own methods and define 
  their implementation.
###Code
Here is an example illustrating dynamic method dispatch:

         // Implementing Dynamic Method Dispatch

         class Apple 
         { 
             void display() 
             { 
                 System.out.println("Inside Apple's display method"); 
             } 
         } 
 
         class Banana extends Apple 
         { 
             void display()   // overriding display()
             { 
                 System.out.println("Inside Banana's display method"); 
             } 
         } 
  
         class Cherry extends Apple 
         { 
             void display()   // overriding display()
             { 
                 System.out.println("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()
             } 
         }

This program creates one superclass (i.e., class Apple) and two subclasses of it (i.e., Banana class and Cherry class). Subclasses Banana and Cherry override the `display()` method declared in Apple. Inside the `main()` method in class `Fruits_Dispatch`, objects of type Apple, Banana, and Cherry are declared. A reference of type Apple, called `ref`, is declared.

The program then assigns a reference to each type of object to `ref` and uses the reference to invoke `display()`.

The version of `display()` executed is determined by the type of the object being referred to at the time of the call.