Skip to main content
๐Ÿ“œ WAYPOINT LESSON

The dispatch table

โญโญ intermediateโณ 12 min read๐Ÿ“ Lesson 117 of 256

Which body actually runs: the object's class decides, constructors excepted.

The exam's inheritance questions all resolve to one question: which method body actually runs? The answer is the object's class, never the reference's type โ€” except constructors.

public class Animal {
    public String speak() { return "..."; }
    public String name() { return "animal"; }
}

public class Dog extends Animal {
    public String speak() { return "Woof"; }   // OVERRIDES
}

Animal a = new Dog();
System.out.println(a.speak());   // "Woof" โ€” the DOG's method
System.out.println(a.name());    // "animal" โ€” inherited, not overridden

a is declared as Animal but holds a Dog: every non-constructor call dispatches on the real object. Write the dispatch table โ€” one row per method, naming the class whose body executes:

| call | body that runs | | ------------ | -------------- | | a.speak() | Dog.speak | | a.name() | Animal.name |

What an override can change: only the body (and it may not weaken visibility). What it cannot change: the signature โ€” change a parameter type and you have overloaded instead, and the superclass version still runs for old calls. This silent-overload trap is the #1 inheritance MCQ.

super.method(args) calls the parent's version explicitly โ€” used when the override extends rather than replaces the parent behavior.