Skip to main content
๐Ÿ“œ 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:

  1. Which methods you may call โ†’ reference type.
  2. Which version runs (for overridden methods) โ†’ object type.
  3. 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.