โ— Shell
clean mode source โ†—

No article detected โ€” showing cleaned page.

Dynamic Method Dispatch or Runtime Polymorphism in Java

Last Updated : 15 Jun, 2026

Dynamic Method Dispatch is the mechanism by which a call to an overridden method is resolved at runtime rather than at compile time. It enables Java to support runtime polymorphism, where a superclass reference can refer to a subclass object, and the method that gets executed depends on the actual object type.

  • Supports late binding (dynamic binding).
  • Enables one interface to represent multiple implementations.
  • Widely used in frameworks and API design for generic programming

Syntax

Parent obj = new Child();
obj.method();

Upcasting in Java

This diagram illustrates upcasting in Java, where a subclass object is assigned to a superclass reference, allowing access to inherited members while supporting runtime polymorphism.

Blank-Diagram-Page-1-4

Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed. Here is an example that illustrates dynamic method dispatch:

Java
class A
{
    void m1()
    {
        System.out.println("Inside A's m1 method");
    }
}

class B extends A
{
    // overriding m1()
    void m1()
    {
        System.out.println("Inside B's m1 method");
    }
}

class C extends A
{
    // overriding m1()
    void m1()
    {
        System.out.println("Inside C's m1 method");
    }
}

// Driver class
class Dispatch
{
    public static void main(String args[])
    {
        // object of type A
        A a = new A();

        // object of type B
        B b = new B();

        // object of type C
        C c = new C();

        // obtain a reference of type A
        A ref;
        
        // ref refers to an A object
        ref = a;

        // calling A's version of m1()
        ref.m1();

        // now ref refers to a B object
        ref = b;

        // calling B's version of m1()
        ref.m1();

        // now ref refers to a C object
        ref = c;

        // calling C's version of m1()
        ref.m1();
    }
}

Output
Inside A's m1 method
Inside B's m1 method
Inside C's m1 method

Explanation : The above program creates one superclass called A and it's two subclasses B and C. These subclasses overrides m1( ) method. Inside the main() method in Dispatch class, initially objects of type A, B, and C are declared.

A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C

Blank Diagram - Page 1 (1)

Now a reference of type A, called ref, is also declared, initially it will point to null.

A ref; // obtain a reference of type A

w

Now we are assigning a reference to each type of object (either A's or B's or C's) to ref, one-by-one, and uses that reference to invoke m1( ). As the output shows, the version of m1( ) executed is determined by the type of object being referred to at the time of the call.

ref = a; // r refers to an A object
ref.m1(); // calling A's version of m1()

q

ref = b; // now r refers to a B object
ref.m1(); // calling B's version of m1()

q

ref = c; // now r refers to a C object
ref.m1(); // calling C's version of m1()

Blank Diagram - Page 1 (3)

Runtime Polymorphism with Data Members

In Java, we can override methods only, not the variables(data members), so runtime polymorphism cannot be achieved by data members.

Java
// class A
class A
{
    int x = 10;
}

// class B
class B extends A
{
    int x = 20;
}

// Driver class
public class Test
{
    public static void main(String args[])
    {
        A a = new B(); // object of type B

        // Data member of class A will be accessed
        System.out.println(a.x);
    }
}

Output
10

Explanation: In above program, both the class A(super class) and B(sub class) have a common variable 'x'. Now we make object of class B, referred by 'a' which is of type of class A. Since variables are not overridden, so the statement "a.x" will always refer to data member of super class.

  • Supports Runtime Polymorphism: The method to execute is determined at runtime based on the actual object type.
  • Improves Flexibility: A parent class reference can point to different child class objects.
  • Enhances Code Reusability: Common code can work with multiple related classes.
  • Promotes Extensibility: New subclasses can be added without modifying existing code.
  • Reduces Code Complexity: Eliminates the need for multiple conditional statements to handle different object types.
  • Encourages Loose Coupling: Programs depend on superclass references rather than specific implementations.

Related article:

Static vs Dynamic binding

Comment
Article Tags:
Article Tags: