Bird
Raised Fist0

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?

hard🎤 Interviewer Follow-up Q10 of Q15
OOP & Design Patterns - Polymorphism - Compile-Time (Overloading) vs Runtime (Overriding)
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:
  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    Calls to overloaded methods not overridden will always invoke superclass versions, possibly causing unexpected behavior -> Option C
  5. Quick Check:

    Only overridden signatures dispatch at runtime; others bind at compile time [OK]
Quick Trick: Only overridden methods dispatch at runtime; others bind statically [OK]
Common Mistakes:
MISTAKES
  • Assuming all overloads override automatically
  • Believing overloaded methods can't be called via superclass
  • Expecting compile-time errors due to overload/override mix
Trap Explanation:
PITFALL
  • Candidates often overlook that only overridden signatures get dynamic dispatch, causing subtle bugs.
Interviewer Note:
CONTEXT
  • Probes deep understanding of method resolution order and polymorphism nuances.
Master "Polymorphism - Compile-Time (Overloading) vs Runtime (Overriding)" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes