INTERACTIVE JAVA LAB Polymorphism & Dynamic Dispatch
Step 1 / 1

Java Hierarchy & Driver

Compiler / System.out Output

Method Lookup Visualizer — Compile-time checks legality; Runtime picks method

1. Stack Variable
(Compile-Time Type)
2. Heap Object
(Actual Runtime Type)
3. Class Method Table
(Dynamic Dispatch vtable)
Reference (javac) Runtime object Dispatched (HIT) Inherited lookup
Ready. Step through the code with Next ▶
Compile-Time vs. Runtime Resolution Rules
Member / ActionWhich Type Decides?When?Why?
Is ref.method() allowed to compile?Reference Type (Left side)Compile time (javac)Compiler only guarantees methods declared in the reference's class/interface
Which overridden instance method runs?Actual Object Type (new ...)Runtime (JVM)Dynamic dispatch follows the object's class pointer to its overridden vtable entry
Which static method or field is accessed?Reference Type (Left side)Compile time (javac)Fields and static methods are not polymorphic—they are hidden, not overridden
Downcasting ((Dog) a).fetch()Checked at runtime by JVMRuntime (JVM)Succeeds if actual object is a Dog; otherwise throws ClassCastException
Classroom Teaching Guide
1. Two-step mantra: Train students to ask two separate questions for every call: (1) Does the compiler allow it? (check left-hand type) and (2) Which version runs? (follow arrow to right-hand new object).
2. Inherited method climb: In Scenario ①, watch a.sleep() check Dog's vtable first, miss, and climb up to Animal.sleep().
3. Controls: Use ← / → keys on desktop or pan the stage horizontally on mobile phones.