Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

// Base class class Shape { constructor(color) { this.color = color; }

getColor() { console.log(Color: ${this.color}); }

area() { console.log('Calculating area...'); } }

// Derived class class Circle extends Shape { constructor(color, radius) { super(color); this.radius = radius; }

area() { const circleArea = Math.PI * this.radius * this.radius; console.log(Circle Area: ${circleArea.toFixed(2)}); } }

// Derived class class Rectangle extends Shape { constructor(color, width, height) { super(color); this.width = width; this.height = height; }

area() { const rectangleArea = this.width * this.height; console.log(Rectangle Area: ${rectangleArea}); } }

// Create instances of the derived classes const redCircle = new Circle('red', 5); const blueRectangle = new Rectangle('blue', 4, 6);

// Use the inherited methods redCircle.getColor(); // Output: Color: red redCircle.area(); // Output: Circle Area: 78.54

blueRectangle.getColor(); // Output: Color: blue blueRectangle.area(); // Output: Rectangle Area: 24

About

The code defines a base class called Shape, which has a constructor that accepts a color parameter and assigns it to the instance property color. It also has two methods, getColor and area.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors