Bird
0
0

You are designing a chess game using polymorphism for piece movement. How should you structure your classes to allow easy addition of new piece types without changing existing code?

hard📝 Trade-off Q15 of 15
LLD - Design — Chess Game
You are designing a chess game using polymorphism for piece movement. How should you structure your classes to allow easy addition of new piece types without changing existing code?
AWrite a single move() function with if-else for each piece type.
BImplement move logic only in the base class and override rarely.
CUse global variables to track piece types and moves.
DCreate a base Piece class with an abstract move() method; each piece subclass implements move().
Step-by-Step Solution
Solution:
  1. Step 1: Apply polymorphism design principle

    Use a base class with an abstract or empty move() method to define interface.
  2. Step 2: Implement subclasses for each piece

    Each piece subclass provides its own move() logic, enabling extension without modifying base code.
  3. Final Answer:

    Create a base Piece class with an abstract move() method; each piece subclass implements move(). -> Option D
  4. Quick Check:

    Base class + subclass move() = scalable design [OK]
Quick Trick: Base class with abstract move() enables easy extension [OK]
Common Mistakes:
  • Using if-else instead of polymorphism
  • Relying on global variables for logic
  • Putting all move logic in base class only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes