w3resource

TypeScript Geometric Shapes - Abstract Classes and Inheritance

TypeScript Classes and OOP : Exercise-16 with Solution

Write a TypeScript program that creates an abstract class GeometricShape with properties like name and abstract methods for calculating area and perimeter. Implement derived classes for specific shapes (e.g., Circle, Rectangle, Triangle) that extend GeometricShape and provide concrete implementations for area and perimeter calculations.

Sample Solution:

TypeScript Code:

// Abstract class GeometricShape
abstract class GeometricShape {
  constructor(public name: string) {}

  // Abstract method for calculating area
  abstract calculateArea(): number;

  // Abstract method for calculating perimeter
  abstract calculatePerimeter(): number;
}

// Derived class Circle
class Circle extends GeometricShape {
  constructor(public name: string, private radius: number) {
    super(name);
  }

  // Implement the calculateArea method for Circle
  calculateArea(): number {
    return Math.PI * this.radius ** 2;
  }

  // Implement the calculatePerimeter method for Circle
  calculatePerimeter(): number {
    return 2 * Math.PI * this.radius;
  }
}

// Derived class Rectangle
class Rectangle extends GeometricShape {
  constructor(public name: string, private width: number, private height: number) {
    super(name);
  }

  // Implement the calculateArea method for Rectangle
  calculateArea(): number {
    return this.width * this.height;
  }

  // Implement the calculatePerimeter method for Rectangle
  calculatePerimeter(): number {
    return 2 * (this.width + this.height);
  }
}

// Derived class Triangle
class Triangle extends GeometricShape {
  constructor(public name: string, private base: number, private height: number) {
    super(name);
  }

  // Implement the calculateArea method for Triangle
  calculateArea(): number {
    return (0.5 * this.base * this.height);
  }

  // Implement the calculatePerimeter method for Triangle (Assuming it's an equilateral triangle)
  calculatePerimeter(): number {
    return 3 * this.base;
  }
}

// Create instances of Circle, Rectangle, and Triangle
const circle = new Circle("Circle", 5);
const rectangle = new Rectangle("Rectangle", 4, 6);
const triangle = new Triangle("Triangle", 3, 4);

// Calculate and print area and perimeter
console.log(`${circle.name} - Area: ${circle.calculateArea()}, Perimeter: ${circle.calculatePerimeter()}`);
console.log(`${rectangle.name} - Area: ${rectangle.calculateArea()}, Perimeter: ${rectangle.calculatePerimeter()}`);
console.log(`${triangle.name} - Area: ${triangle.calculateArea()}, Perimeter: ${triangle.calculatePerimeter()}`);

Explanations:

In the exercise above -

  • First, we define an abstract class "GeometricShape" with properties 'name' and abstract methods "calculateArea()" and "calculatePerimeter()".
  • Create three derived classes (Circle, Rectangle, and Triangle) that extend "GeometricShape" and provide their own implementations for calculating area and perimeter based on their specific shapes.
  • Create instances of each shape, passing in relevant details like dimensions.
  • Finally, calculate and print the area and perimeter for each shape.

Output:

"Circle - Area: 78.53981633974483, Perimeter: 31.41592653589793"
"Rectangle - Area: 24, Perimeter: 20"
"Triangle - Area: 6, Perimeter: 9"

TypeScript Editor:

See the Pen TypeScript by w3resource (@w3resource) on CodePen.


Previous: TypeScript Employee Hierarchy - Abstract Classes and Inheritance.
Next: TypeScript Student Class with Private and Protected Properties.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/typescript-exercises/typescript-class-and-oop-exercise-16.php