Bird
0
0

Given a class hierarchy where Piece has a method move() and King overrides it, what will happen when calling move() on a King instance?

medium📝 Analysis Q4 of 15
LLD - Design — Chess Game
Given a class hierarchy where Piece has a method move() and King overrides it, what will happen when calling move() on a King instance?
AThe <code>Piece</code> class's <code>move()</code> method is executed
BThe <code>King</code> class's <code>move()</code> method is executed
CBoth <code>King</code> and <code>Piece</code> methods execute automatically
DA runtime error occurs due to method conflict
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding

    When a subclass overrides a method, calling it on subclass instance uses the subclass version.
  2. Step 2: Confirm behavior on King instance

    Calling move() on King runs King's overridden method, not the base Piece method.
  3. Final Answer:

    The King class's move() method is executed -> Option B
  4. Quick Check:

    Overridden method call = subclass method [OK]
Quick Trick: Subclass method overrides base method when called [OK]
Common Mistakes:
  • Assuming base method runs instead
  • Thinking both methods run automatically
  • Expecting runtime error on override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes