๐ WAYPOINT LESSON
Two answers in one variable
โญโญโญ advancedโณ 10 min read๐ Lesson 193 of 256
Reference type governs what you may name; object type governs what runs; fields dispatch by neither.
Every object reference question has two answers hiding in one variable: what the reference type allows (compile-time) and what the object type does (runtime). Write both above any tricky line:
Employee e = new Manager("Ann", 100, 50);
// reference type: Employee โ compiler allows e.pay(), forbids e.bonus
// object type: Manager โ pay() dispatches to the OVERRIDE
Three laws follow:
- Which methods you may call โ reference type.
- Which version runs (for overridden methods) โ object type.
- Fields are not dispatched: field access is resolved by reference type. Shadowed fields are the exam's favorite trap.
instanceof checks the object type (including subclasses); a cast
changes only what the compiler lets you name โ it never changes the
object.