Method Overriding

Method Overriding
πŸ‘¨β€πŸ’Ό Now let's add methods to our shape hierarchy. Each shape will override a method to provide its own implementation.
The Shape, Circle, and Rectangle classes from the previous exercise are already defined. You'll add and override the getArea() method.
🐨 Open
index.ts
and:
  1. Add getArea(): number on Shape that returns 0
  2. Override getArea() on Circle so a circle's area is Ο€ Γ— radiusΒ² (use Math.PI)
  3. Override getArea() on Rectangle so a rectangle's area is width Γ— height
  4. Keep exporting: export { Shape, Circle, Rectangle }

Fixtures and success criteria

const shape = new Shape('red')
const circle = new Circle('red', 5)
const rectangle = new Rectangle('blue', 10, 20)
  • shape.getArea() === 0
  • circle.getArea() is about 78.54 (within 0.01 of Math.PI * 25)
  • rectangle.getArea() === 200
  • Circle and rectangle areas are both greater than 0 and not equal to each other

Please set the playground first

Loading "Method Overriding"
Loading "Method Overriding"