If you extend a class with multiple overloaded methods and override only one of them in a subclass, what is a potential pitfall when calling these methods through a superclass reference?
If you extend a class with multiple overloaded methods and override only one of them in a subclass, what is a potential pitfall when calling these methods through a superclass reference?
AOverloaded methods cannot be called through superclass references if any are overridden.
BAll overloaded methods become overridden automatically once one is overridden.
CCalls to overloaded methods not overridden will always invoke superclass versions, possibly causing unexpected behavior.
DThe compiler will throw an error due to ambiguity between overloaded and overridden methods.
Step-by-Step Solution
Solution:
Step 1: Understand method resolution with overloading and overriding
Overriding affects only methods with matching signatures; overloaded methods with different parameters remain bound to superclass versions when called via superclass reference.
Step 2: Analyze implications
Calls to overloaded methods not overridden will not dispatch to subclass, potentially causing unexpected behavior if subclass intended to extend all variants.
Step 3: Evaluate options
A is incorrect; overloaded methods can be called via superclass references. B is false; overriding one method does not affect others. C is correct; calls to overloaded methods not overridden invoke superclass versions, possibly causing unexpected behavior. D is wrong; no compile-time ambiguity arises here.
Final Answer:
Calls to overloaded methods not overridden will always invoke superclass versions, possibly causing unexpected behavior -> Option C
Quick Check:
Only overridden signatures dispatch at runtime; others bind at compile time [OK]