Explorer
Node.js

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; } } ```

Class Inheritance

Medium
Create a `Shape` base class with `area()` method, then `Rectangle` and `Circle` subclasses that implement area calculation.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"rect":15,"circle":153.94,"isShape":true}