Bird
Raised Fist0

Identify the error in this piece hierarchy code snippet:

medium📝 Analysis Q14 of Q15
LLD - Design — Chess Game
Identify the error in this piece hierarchy code snippet:
class Piece {
  constructor(type, position) {
    this.type = type;
    this.position = position;
  }
}
class Queen extends Piece {
  constructor(position) {
    this.type = 'Queen';
    this.position = position;
  }
}
APosition should not be passed to constructor
BQueen class should not have a constructor
CMissing call to super() in Queen constructor
DType should be passed as parameter to Queen constructor
Step-by-Step Solution
Solution:
  1. Step 1: Review subclass constructor rules

    In subclasses, the constructor must call super() before using this.
  2. Step 2: Check Queen constructor

    Queen constructor assigns this.type and this.position without calling super(), causing an error.
  3. Final Answer:

    Missing call to super() in Queen constructor -> Option C
  4. Quick Check:

    Subclass constructor must call super() first [OK]
Quick Trick: Always call super() before using this in subclass constructor [OK]
Common Mistakes:
MISTAKES
  • Forgetting super() call in subclass constructor
  • Trying to assign this before super()
  • Assuming constructor is optional in subclass

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes