Extends

Extends
πŸ‘¨β€πŸ’Ό Let's build a shape hierarchy. We'll create a base Shape class and extend it with specific shapes like Circle and Rectangle.
🐨 Open
index.ts
and:
  1. Create a Shape base class with:
    • Public field: color (string)
    • Constructor (color: string)
  2. Create a Circle class that extends Shape with:
    • Public field: radius (number)
    • Constructor (color, radius) that initializes the parent via super(color)
  3. Create a Rectangle class that extends Shape with:
    • Public fields: width (number), height (number)
    • Constructor (color, width, height) that calls super(color)
  4. Export all three: export { Shape, Circle, Rectangle }

Fixtures and success criteria

const circle = new Circle('red', 5)
const rectangle = new Rectangle('blue', 10, 20)
  • circle.color === 'red' and circle.radius === 5
  • rectangle.color === 'blue', width === 10, height === 20
  • circle instanceof Shape and rectangle instanceof Shape are both true (each subclass is substitutable as a Shape)

Please set the playground first

Loading "Extends"
Loading "Extends"
Login to get access to the exclusive discord channel.
Loading Discord Posts