Java Hierarchy & Driver
Compiler / System.out Output
Method Lookup Visualizer — Compile-time checks legality; Runtime picks method
Reference (javac) Runtime object Dispatched (HIT) Inherited lookup
Ready. Step through the code with Next ▶
Compile-Time vs. Runtime Resolution Rules
| Member / Action | Which 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 JVM | Runtime (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
2. Inherited method climb: In Scenario ①, watch
3. Controls: Use ← / → keys on desktop or pan the stage horizontally on mobile phones.
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.