unkletayo
12

Polymorphism and dynamic dispatch

Why the runtime type decides which method runs, not the declared type.

Adetayo Akinsanya2026-08-185 min read

  • Java
  • Core Java
  • OOP
  • Polymorphism
  • Dynamic Dispatch

Part 12 of the series.Read Inheritance: extending a class (Part 11) first if you have not.

In this article (7 sections)
  1. Declared type versus runtime type
  2. Dynamic dispatch
  3. How HotSpot implements dispatch: the vtable
  4. instanceof and type checks
  5. Casting
  6. The design implication
  7. Prediction exercise
Account a = new SavingsAccount("S001", 1000, 0.05);
System.out.println(a.getClass().getSimpleName());  // SavingsAccount
a.applyInterest();                                  // compile error

The variable a is declared as Account. The object it holds is a SavingsAccount. The JVM knows the runtime type; the compiler only knows the declared type. This distinction drives most of how objects behave in Java.

Declared type versus runtime type

A variable’s declared type is what the compiler sees, the type in the declaration. A variable’s runtime type is the actual class of the object it holds.

Account a = new SavingsAccount("S001", 1000, 0.05);

Declared type: Account. Runtime type: SavingsAccount.

The compiler uses the declared type to decide what methods and fields are accessible. Account does not declare applyInterest(), so the compiler rejects the call, even though the object is a SavingsAccount. The JVM uses the runtime type to decide which implementation of a method to invoke.

Dynamic dispatch

When you call an overridden method, Java always invokes the version that belongs to the runtime type.

Account[] accounts = {
    new Account("C001", 500),
    new SavingsAccount("S001", 1000, 0.05),
    new SavingsAccount("S002", 2000, 0.03)
};

for (Account a : accounts) {
    System.out.println(a.getBalance());
}

Every call a.getBalance() goes through the same declared type (Account) but each object knows its own class. If SavingsAccount overrides getBalance(), the overriding version runs for the savings accounts. If it does not override, the Account version runs. The loop does not need to know or check which type each element is.

This is called dynamic dispatch (or virtual dispatch): the method that runs is chosen at runtime based on the actual object, not at compile time based on the variable type. It enables code that operates on a declared type to work correctly with any subclass of that type.

How HotSpot implements dispatch: the vtable

As a HotSpot implementation detail (not a JLS guarantee): each class has a vtable, a table of method pointers with one entry per overridable method. When a subclass overrides a method, its vtable entry points to the overriding method. When the JVM calls a virtual method, it looks up the vtable of the object’s actual class and jumps to the method there.

For a.getBalance() on a SavingsAccount, the JVM looks at the SavingsAccount vtable. If SavingsAccount overrides getBalance, the vtable entry points to that version; otherwise it points to Account’s version. The lookup takes essentially constant time.

JIT compilation replaces many vtable lookups with direct calls when the JIT can prove only one implementation is ever called at a given call site (monomorphic dispatch). The vtable mechanism is what makes the guarantee possible in the first place.

instanceof and type checks

Sometimes you need to know the runtime type explicitly, for example to call a method that the declared type does not expose.

The instanceof operator tests whether an object is an instance of a given class:

for (Account a : accounts) {
    if (a instanceof SavingsAccount) {
        SavingsAccount s = (SavingsAccount) a;
        s.applyInterest();
    }
}

Since Java 16, pattern matching for instanceof combines the test and the cast into one statement:

for (Account a : accounts) {
    if (a instanceof SavingsAccount s) {
        s.applyInterest();
    }
}

If the test passes, s is bound to the object as a SavingsAccount and is available within the if block. The explicit cast is gone. The variable s is only in scope where the compiler can verify the test has passed.

Casting

A cast tells the compiler to treat a reference as a different type. The compiler trusts you; the JVM checks at runtime.

Account a = new Account("C001", 500);
SavingsAccount s = (SavingsAccount) a;  // compiles
// throws ClassCastException at runtime

The cast compiles because Account and SavingsAccount are related types. It throws ClassCastException at runtime because the actual object is not a SavingsAccount. Always use instanceof to guard a cast when the type is not certain.

A cast does not convert an object. It tells the JVM to interpret the reference as a different type. The object itself does not change.

The design implication

Dynamic dispatch is what makes it possible to write a for loop over Account[] without caring whether each element is a plain account, a savings account, or any future subclass. The loop calls the right method automatically. New subclasses can be added without changing the loop.

This only works when the subclass genuinely behaves like the superclass, meaning you can use a SavingsAccount wherever an Account is expected without breaking the caller’s assumptions. That principle, that a subclass should be substitutable for its superclass, is what makes polymorphism useful rather than just surprising. Inheritance covers when that substitutability holds and when to prefer composition instead.

Prediction exercise

class Shape {
    public double area() { return 0; }
}

class Circle extends Shape {
    double radius;
    Circle(double radius) { this.radius = radius; }

    @Override
    public double area() { return Math.PI * radius * radius; }
}

class Square extends Shape {
    double side;
    Square(double side) { this.side = side; }

    @Override
    public double area() { return side * side; }
}

Shape[] shapes = { new Circle(3), new Square(4), new Circle(1) };
double total = 0;
for (Shape s : shapes) {
    total += s.area();
}
System.out.printf("%.2f%n", total);

Predict the output. Then: if you add a new Triangle class that extends Shape and overrides area(), does the loop need to change?

Questions or corrections

Found a mistake or have a question about this breakdown? Reach out directly.

Reply on X (@unkletayor)Email feedback