100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Polymorphic Dispatch?

Polymorphic dispatch explained — how runtime type resolves overridden methods via vtables, with Java examples and interview Q&A.

mediumQ128 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Polymorphic dispatch is the runtime mechanism by which a call to a method through a base-type reference or interface is routed to the specific implementation defined by the actual (runtime) type of the object, not the declared type of the reference.

When code calls animal.makeSound() on a variable declared as Animal but holding a Dog instance, polymorphic dispatch looks up makeSound in Dog’s vtable (or equivalent dispatch table) at runtime and invokes Dog’s override rather than Animal’s. This is what makes runtime polymorphism work: the same call site produces different behavior depending on which concrete object is behind the reference. It contrasts with static dispatch, where the compiler resolves the call target purely from the declared type at compile time. Most object-oriented languages implement it via a vtable pointer stored in each object, or via interface dispatch tables for interface calls.

  • Lets one call site handle many concrete types uniformly
  • Enables extensible designs — new subclasses work without changing calling code
  • Underpins the Open/Closed Principle in OOP design
  • Decouples callers from concrete implementation details

AI Mentor Explanation

A captain shouts “bowl()” to whichever bowler is currently at the top of their mark, and the actual delivery — a yorker, a googly, a bouncer — depends entirely on which specific bowler is standing there, not on the generic instruction given. The captain’s call site never changes; only the runtime identity of the bowler determines what actually happens. That is polymorphic dispatch: the same call is routed to different concrete behavior based on the real object executing it, decided only at the moment of the call.

Step-by-Step Explanation

  1. Step 1

    Declare a base-type reference

    A variable is typed as a base class or interface (e.g. Animal a).

  2. Step 2

    Assign a concrete subtype instance

    The reference actually points to a subclass object at runtime (e.g. new Dog()).

  3. Step 3

    Call the overridden method

    Code calls a.makeSound() using only the base-type reference.

  4. Step 4

    Runtime resolves via vtable lookup

    The runtime consults the object’s actual type’s dispatch table and invokes that type’s override.

What Interviewer Expects

  • Clear distinction between declared (static) type and actual (runtime) type
  • Mention of the vtable or equivalent dispatch-table mechanism
  • Contrast with static/compile-time dispatch (overloading)
  • A concrete code example showing base-type reference, subclass instance, call

Common Mistakes

  • Confusing polymorphic dispatch with method overloading (which is compile-time)
  • Saying the compiler decides which override runs (it is the runtime, via the vtable)
  • Forgetting that static and private methods do not participate in polymorphic dispatch
  • Not explaining why this enables extensibility without modifying calling code

Best Answer (HR Friendly)

Polymorphic dispatch is what lets you call the same method name through a general reference and have the correct, specific behavior run automatically, based on what the actual object really is underneath. So calling makeSound() on something declared as Animal will bark if it’s really a Dog and meow if it’s really a Cat, and that decision happens at runtime, not when the code is compiled.

Code Example

Polymorphic dispatch via a base-type reference
abstract class Animal {
    abstract void makeSound();
}

class Dog extends Animal {
    void makeSound() { System.out.println("Bark"); }
}

class Cat extends Animal {
    void makeSound() { System.out.println("Meow"); }
}

Animal[] animals = { new Dog(), new Cat() };
for (Animal a : animals) {
    a.makeSound(); // resolved by actual runtime type, not declared type
}
// Output: Bark
//         Meow

Follow-up Questions

  • How does polymorphic dispatch differ from method overloading?
  • What role does a vtable play in implementing dynamic dispatch?
  • Why can’t static methods participate in polymorphic dispatch?
  • How does polymorphic dispatch support the Open/Closed Principle?

MCQ Practice

1. Polymorphic dispatch resolves which method implementation runs based on?

Dynamic dispatch uses the object’s real runtime type to select the implementation, not the reference’s declared type.

2. Which mechanism commonly implements polymorphic dispatch under the hood?

Most OOP runtimes store a pointer to a vtable in each object, used to look up the correct override at call time.

3. Which of these does NOT participate in polymorphic dispatch?

Static methods are resolved at compile time by declared type and do not use dynamic dispatch.

Flash Cards

Polymorphic dispatch in one line?Runtime routing of a method call to the implementation matching the object’s actual type.

Resolved when?At runtime, based on the actual object, not the declared reference type.

Common implementation mechanism?A vtable (virtual method table) pointer stored per object.

Contrast with overloading?Overloading is compile-time; polymorphic dispatch is runtime.

1 / 4

Continue Learning