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
and:
- Add
getArea(): numberonShapethat returns0 - Override
getArea()onCircleso a circle's area is Ο Γ radiusΒ² (useMath.PI) - Override
getArea()onRectangleso a rectangle's area is width Γ height - 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() === 0circle.getArea()is about78.54(within0.01ofMath.PI * 25)rectangle.getArea() === 200- Circle and rectangle areas are both greater than
0and not equal to each other