DYNAMIC METHOD DISPATCH JAVA


Dynamic Method Dispatch In JAVA | Core Java Tutorial | Minigranth

Dynamic Method Dispatch : Introduction

  • We have already discussed before that method overriding is the way to implement polymorphism, more specifically run-time polymorphism also called dynamic method dispatch .
  • Dynamic Method Dispatch is the way in which the call to an overridden method is resolved at run-time rather than compile time. Hence called runtime polymorphism.
  • Resolving call at run-time means that compiler doesn’t know the type of object that is referring to overridden method until run-time.
  • In Dynamic Method Dispatch call to the overridden method is resolved by the reference variable of parent class referring the object of child class.

Dynamic Method Dispatch : Example


This image describes the sample program supporting the concept of Dynamic Method Dispatch.
Dynamic Method Dispatch : Example

This image describes the output of sample program supporting the concept of Dynamic Method Dispatch.
Dynamic Method Dispatch : Output

  • In above example we have created a parent class reference variable (ref)which refers to the object of child class and then call methods of each class by having different reference to object every time.