01 / 21

Dynamic Dispatch
in Java

Learn how Java decides which method to call at runtime, enabling polymorphism and flexible object-oriented design.

Java

What Is Dynamic Dispatch?

When you call a method on an object in Java, the JVM must decide which version of that method to execute. Dynamic dispatch is the mechanism by which this decision is made at runtime, based on the actual type of the object: not the declared type of the variable holding it.

Key Vocabulary

Declared Type
The type written on the left side of a variable declaration (compile-time type).
Actual Type
The type of the object created with new (runtime type).
Overriding
A subclass providing its own implementation of a method defined in its superclass.

Why Does It Matter?

1
Polymorphism: Write code that works with a superclass, and it automatically handles any subclass.
2
Extensibility: Add new subclasses without changing existing code that uses the superclass.
3
Real-world modeling: Objects behave according to what they are, not what the variable says they might be.

The Core Rule

Compile Time Check: does declared type have this method? Uses DECLARED type Runtime Execute: which version of the method to run? Uses ACTUAL type Animal a = new Dog(); a. speak(); Declared: Animal Actual: Dog // calls Dog.speak()

How the JVM Resolves a Call

a.speak() is called COMPILE TIME Does Animal have speak()? ✓ RUNTIME What is the actual type? Dog DISPATCH Does Dog override speak()? Run Dog.speak() If Dog does not override, walk up to parent until found

Static vs Dynamic Binding

Static Binding Dynamic Binding
When resolved Compile time Runtime
Applies to Overloaded methods, static methods, private methods Overridden instance methods
Based on Declared (reference) type Actual (object) type
Keyword clue static, private, final Instance methods + inheritance
Performance Slightly faster (no lookup) Tiny overhead (vtable lookup)

How to Trace a Method Call

1
Identify the declared type of the variable (left of the =).
2
Identify the actual type of the object (after new).
3
Compile-time check: Does the declared type (or its ancestors) have this method signature? If not, compile error.
4
Runtime dispatch: Starting at the actual type, look for an override. If found, run it. If not, walk up the hierarchy.

Example 1: Basic Dispatch

class Animal { void speak() { System.out.println("..."); } } class Dog extends Animal { void speak() { System.out.println("Woof!"); } } Animal a = new Dog(); a.speak(); // Output: Woof!

Example 2: Three-Level Hierarchy

class Shape { String name() { return "Shape"; } } class Polygon extends Shape { String name() { return "Polygon"; } } class Triangle extends Polygon { // does NOT override name() } Shape s = new Triangle(); System.out.println(s.name()); // Output: Polygon

Example 3: Interfaces and Dispatch

interface Drawable { void draw(); } class Circle implements Drawable { public void draw() { System.out.println("Drawing circle"); } } class Square implements Drawable { public void draw() { System.out.println("Drawing square"); } } Drawable d = new Circle(); d.draw(); // Output: Drawing circle d = new Square(); d.draw(); // Output: Drawing square

The Catch: Fields Are Not Dispatched

class Parent { String label = "Parent"; } class Child extends Parent { String label = "Child"; } Parent p = new Child(); System.out.println(p.label); // Output: Parent

Static Methods: No Dispatch

class Base { static void greet() { System.out.println("Hello from Base"); } } class Sub extends Base { static void greet() { System.out.println("Hello from Sub"); } } Base b = new Sub(); b.greet(); // Output: Hello from Base

Dispatch Walk-Up Diagram

Object Vehicle start(), stop() Car start() overridden Bike no overrides Vehicle v = new Car(); v.start() → Car.start() ✓ Vehicle v = new Bike(); v.start() → Vehicle.start() Bike has no override, so the JVM walks up to Vehicle

Common Mistakes

✕ Confusing Fields with Methods
Assuming that fields are dispatched dynamically like methods. Fields use the declared type, not the actual type.
✕ Static = Dynamic
Thinking static methods are dispatched at runtime. Static methods are resolved at compile time based on declared type.
✕ Forgetting the Walk-Up
Assuming a method must exist in the actual class itself. If not overridden, the JVM walks up the hierarchy to find it.

Summary Reference

Feature Binding Based On
Instance methods (overridden) Dynamic (runtime) Actual type
Static methods Static (compile time) Declared type
Private methods Static (compile time) Declared type
Final methods Static (compile time) Declared type
Fields (instance variables) Static (compile time) Declared type
Overloaded methods Static (compile time) Declared type + parameter types

Question 1 of 5

What is the output of a.speak()?

Question 2 of 5

What is the output of obj.hello()?

Question 3 of 5

Which of the following is required for dynamic dispatch to occur in Java?

Question 4 of 5

What is the output?

Question 5 of 5

What is the output of m.play()?