Trace the sequence of method resolution when a subclass object calls an overridden method via a superclass reference variable. What happens step-by-step internally?
Trace the sequence of method resolution when a subclass object calls an overridden method via a superclass reference variable. What happens step-by-step internally?
AThe compiler binds the method call to the superclass method at compile time, and the superclass method executes at runtime
BThe compiler generates multiple versions of the method for each subclass, and the correct one is chosen at compile time
CThe compiler defers binding until runtime, where the object's actual class method is looked up via the vtable and executed
DThe method call is resolved by matching the method signature with the parameter types at runtime
Step-by-Step Solution
Step 1: Compile-time binding
The compiler knows the reference type but defers binding for overridden methods to runtime.
Step 2: Runtime dispatch
At runtime, the actual object's class is identified, and the method pointer is retrieved from the vtable.
Step 3: Method execution
The overridden method in the subclass is executed, enabling dynamic polymorphism.
Final Answer:
Option C -> Option C
Quick Check:
Overriding uses dynamic dispatch via vtable lookup at runtime, not compile-time binding.
Quick Trick:Overriding = runtime binding via vtable lookup
Common Mistakes:
MISTAKES
Assuming compile-time binding for overridden methods
Confusing overloading resolution with overriding
Believing method signature matching happens at runtime
Trap Explanation:
PITFALL
Option A incorrectly claims compile-time binding for overridden methods; option B confuses overloading with overriding; option D wrongly states runtime signature matching.
Interviewer Note:
CONTEXT
Checks candidate's understanding of dynamic dispatch and method resolution order in overriding.
Master "Polymorphism - Compile-Time (Overloading) vs Runtime (Overriding)" in OOP & Design Patterns
2 interactive learning modes - each teaches the same concept differently