Create a `Shape` base class with `area()` method, then `Rectangle` and `Circle` subclasses that implement area calculation.
Problem Statement
Create a `Shape` base class with `area()` method, then `Rectangle` and `Circle` subclasses that implement area calculation.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Use class and extends for inheritance.
š” Hint 2:
Call super() in the subclass constructor.
š” Hint 3:
Rectangle area = width * height. Circle area = Math.PI * radius^2.
ā
Solution:
```javascript
class Shape {
area() { return 0; }
}
class Rectangle extends Shape {
constructor(w, h) { super(); this.w = w; this.h = h; }
area() { return this.w * this.h; }
}
class Circle extends Shape {
constructor(r) { super(); this.r = r; }
area() { return Math.PI * this.r * this.r; }
}
```