Dynamic Method Dispatch in Java

Mehvish Ashiq Oct 12, 2023
  1. Dynamic Method Dispatch in Java
  2. Importance of Dynamic Method Dispatch in Java
  3. Dynamic Method Dispatch with Code Example in Java
Dynamic Method Dispatch in Java

This tutorial educates about the dynamic method dispatch in Java. It speaks about its technical definition and importance and explains with code examples.

Dynamic Method Dispatch in Java

Let’s break down the words and think of Dispatch as deciding which function (method) to call. The word Dynamic tells that it’s determined at the runtime.

In the simplest words, we can say which function/method should be executed is decided at runtime.

Considering technical definition, the dynamic method dispatch (also known as runtime polymorphism) is a mechanism that is used to resolve a call to an overridden method at runtime instead of compile time.

When we call the overridden method in the child class by using a reference, Java decides which method would be executed depending on the object’s type to which it refers. Let’s understand the definition by using the following two screenshots of code.

Focus on the following screenshot where we have three classes named Shape, Rectangle, and the Main class. The Shape is the superclass, and Rectangle is the child class.

The main method has two objects of the Shape and Rectangle type and saves their references in shape and rectangle variables.

These variables call the respective class’ display() method. For instance, if we call shape.display(), it would be decided at compile-time that the display() of the Shape class is being called because the shape contains the reference of the Shape class.

dynamic method dispatch in java - definition part one

Now, change the code as given in the following screenshot. Here, the main method contains the two objects, one for the Shape class and the other for the Rectangle class but both (the variable shape and rectangle) contain the reference of the Shape class.

So, how the program decides which overridden method should be called? This is where dynamic method dispatch plays its role.

Here, Java determines which method should be executed depending on the type of object it refers to.

If the object is Rectangle type, then the display() from the Rectangle class would be called, and if the object is of Shape type, then the display() method of the Shape class would be called. And all these decisions are made at runtime.

This is what we call the runtime polymorphism or dynamic method dispatch.

dynamic method dispatch in java - definition part two

Importance of Dynamic Method Dispatch in Java

The following points increase the importance of using the dynamic method dispatch.

  1. The dynamic method dispatch lets the Java support method overriding necessary for the runtime polymorphism.
  2. It lets the child class incorporate their functions and update the implementation as per project requirements.
  3. It lets the superclass define a function/method that would be shared with its child classes and allow these child classes to update the function’s implementation.

Dynamic Method Dispatch with Code Example in Java

Example Code:

class Shape {
  Shape() {}
  void display() {
    System.out.println("I am in the Shape class");
  }
}

class Rectangle extends Shape {
  Rectangle() {}
  void display() {
    System.out.println("I am in the Rectangle class");
  }
}

class Triangle extends Shape {
  Triangle() {}
  void display() {
    System.out.println("I am in the Triangle class");
  }
}

public class Main {
  public static void main(String args[]) {
    Shape rectangle = new Rectangle();
    Shape triangle = new Triangle();

    rectangle.display();
    triangle.display();
  }
}

Output:

I am in the Rectangle class
I am in the Triangle class

Here, the Rectangle and Triangle classes extend the Shape class. These classes have a display() method that we are supposed to call the main method.

The main method has two objects, one for the Rectangle type and the other for the Triangle type, but the reference variables rectangle and triangle refer to the same parent class named Shape.

How to find out which function would be called? Here, the dynamic dispatch method comes into the picture.

Remember, we are also using dynamic binding and upcasting in the following two codes of code.

Shape rectangle = new Rectangle();
Shape triangle = new Triangle();

The dynamic binding uses the object (instance of the class, which is new Rectangle() and new Triangle() here) to resolve the method call at runtime.

Here the upcasting is also used because the parent class reference variables (rectangle and triangle) are referencing the child classes Rectangle and Triangle.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - Java Method