Bird
Raised Fist0

If a class overloads a method and also overrides it in a subclass, which method call resolution scenario can cause unexpected behavior, and why?

hard🎤 Interviewer Follow-up Q15 of Q15
OOP & Design Patterns - Polymorphism - Compile-Time (Overloading) vs Runtime (Overriding)
If a class overloads a method and also overrides it in a subclass, which method call resolution scenario can cause unexpected behavior, and why?
ACalling an overloaded method with parameters matching the superclass signature may invoke the superclass version even if the subclass overrides another variant
BOverloaded methods are resolved at runtime, so the most specific subclass method is always called
CCalling the overloaded method via a superclass reference always invokes the subclass's overridden method, ignoring overloading
DOverriding disables overloading in subclasses, so only one method version is callable
Step-by-Step Solution
  1. Step 1: Recall overloading vs overriding resolution

    Overloading is resolved at compile time based on reference type and parameter list; overriding is resolved at runtime based on object type.
  2. Step 2: Analyze call with superclass reference

    If the method call matches an overloaded method signature declared in the superclass, that method is chosen at compile time.
  3. Step 3: Consider overridden methods

    If the subclass overrides a different method variant, it won't affect calls resolved to the superclass's overloaded method signature.
  4. Step 4: Identify unexpected behavior

    This can cause the superclass method to be called unexpectedly, even if the subclass has an overridden variant with a different signature.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    Overloading resolution depends on reference type and parameters, potentially bypassing subclass overrides.
Quick Trick: Overloading resolved by reference type; overriding by object type
Common Mistakes:
MISTAKES
  • Assuming overloaded methods are dynamically dispatched
  • Believing overriding disables overloading
  • Thinking subclass overrides affect all overloaded variants
Trap Explanation:
PITFALL
  • Options B, C, and D are incorrect about overloading and overriding resolution.
Interviewer Note:
CONTEXT
  • Tests deep understanding of combined overloading and overriding interactions and their resolution order.
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