Java

What is Dynamic Method Dispatch in Java

In Java Object Oriented Programming(OOP), there can be instances where the programmer needs to eliminate the overriding exceptions to maintain the associativity in code. For instance, refraining from the limitations and maintaining the relativity in the code simultaneously. In such situations, “Dynamic Method Dispatch” in Java assists in streamlining the code functionalities at the developer’s end.

This blog will discuss the implementation of Dynamic Method Dispatch in Java.

What is “Dynamic Method Dispatch” in Java?

Dynamic Method Dispatch” is the methodology in which an overridden method is coped with the help of reference at run time. This can be achieved by overriding a method via inheritance and resolving the overriding exception in the main via “referencing”.

Implementation of “Dynamic Method Dispatch” in Java

In the following code, the “Dynamic Method Dispatch” mechanism can be applied to cope with the overriding limitation:

class parentClass{
 public void display() {
  System.out.println("This is parent display function");   
}}
class childClass extends parentClass{
 public void display() {
  System.out.println("This is child display function");
}}
parentClass object1 = new parentClass();
parentClass object2 = new childClass();
parentClass ref;
ref = object1;
object1.display();
ref = object2;
object2.display();

In the above code snippet:

  • Firstly, create a parent(super) class named “parentClass”.
  • Within the class, define a function named “display()” and log the stated message on the console.
  • In the next step, inherit a child class named “childClass” via the “extends” keyword.
  • In the child class, override the parent class function “display()” and display the provided message.
  • Now, in main, create the objects of both the parent and child classes via the “new” keyword and the “parentClass()” and “childClass()” constructors, respectively.
  • After that, refer to the parent class object and invoke the accumulated function within the class.
  • Likewise, refer to the child class object and similarly access the overridden function “display()” via object.
  • This will resultantly resolve the overriding limitation via reference.

Output

In the above outcome, it is evident that both the default and the overridden functionalities can be accessed.

Conclusion

Dynamic Method Dispatch” in Java is the methodology in which an overridden method is coped with the help of reference at run time. This approach can be utilized to cancel out the overriding mechanism, thereby resolving the faced exception and invoking the functionalities of both the super(parent) and sub(child) classes simultaneously. This blog discussed the implementation of dynamic method dispatch in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.