Bird
Raised Fist0

In a class hierarchy where a subclass both overloads and overrides a method from its superclass, under which circumstance can invoking the method through a superclass reference call the overloaded method instead of the overridden one?

hard🚀 Advanced Concept Q8 of Q15
OOP & Design Patterns - Polymorphism - Compile-Time (Overloading) vs Runtime (Overriding)
In a class hierarchy where a subclass both overloads and overrides a method from its superclass, under which circumstance can invoking the method through a superclass reference call the overloaded method instead of the overridden one?
AWhen the overloaded method is private in the subclass.
BWhen the subclass method is marked as final, preventing overriding.
CWhen the superclass reference is explicitly cast to the subclass type before the call.
DWhen the method call's argument types match the overloaded method's signature but not the overridden method's signature.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Overriding vs Overloading

    Overriding replaces a method with the same signature; overloading adds methods with different signatures.
  2. Step 2: Method Resolution

    At compile time, the method signature determines which overloaded method is selected; at runtime, overriding determines which implementation runs.
  3. Step 3: Analyze the Call

    If the method call matches an overloaded method signature not overridden, the overloaded method is called via the superclass reference.
  4. Final Answer:

    When the method call's argument types match the overloaded method's signature but not the overridden method's signature -> Option D
  5. Quick Check:

    Compile-time signature matching selects overloaded method [OK]
Quick Trick: Compile-time signature decides overloaded method call [OK]
Common Mistakes:
MISTAKES
  • Assuming overridden methods always execute regardless of signature.
  • Confusing method visibility modifiers with overloading behavior.
  • Believing casting always changes method resolution.
Trap Explanation:
PITFALL
  • Overriding applies only when signatures match; overloading depends on compile-time signature matching.
Interviewer Note:
CONTEXT
  • Tests deep understanding of method resolution order with overloading and overriding combined.
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