Open In App

Typescript Abstract Class

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript, a superset of JavaScript, introduces many features to improve code organization, readability, and maintainability. One of these features is abstract classes. This article will explain what abstract classes are, how they work, and how to use them effectively in your TypeScript projects.

These are the topics that we are going to discuss:

What is an Abstract Class?

An abstract class is a class that cannot be instantiated directly. Instead, it serves as a blueprint for other classes. Abstract classes are used to define common behaviors that multiple derived classes can share. They can contain both implementation and abstract (unimplemented) methods.

How to define an Abstract Class?

To define an abstract class in TypeScript, use the abstract keyword. Here's a basic example:

abstract class Animal {
abstract makeSound(): void; // Abstract method

move(): void {
console.log("Moving...");
}
}

In this example, Animal is an abstract class with an abstract method makeSound and a regular method move. The makeSound method does not have an implementation, so any class that extends Animal must provide an implementation for makeSound.

Basic Usage of Abstract Classes

This TypeScript code defines an abstract Animal class with a constructor that sets the name property, an abstract makeSound() method, and a move() method that logs a movement message. The Dog and Cat classes extend Animal, implementing the makeSound() method with species-specific sounds. Instances of Dog and Cat are created, and their makeSound() and move() methods output appropriate messages for each animal.

Example: This example illustrates the basic usage of abstract classes.

JavaScript
abstract class Animal {
    constructor(public name: string) { }

    abstract makeSound(): void;

    move(): void {
        console.log(`${this.name} is moving.`);
    }
}

class Dog extends Animal {
    makeSound(): void {
        console.log(`${this.name} says: Woof!`);
    }
}

class Cat extends Animal {
    makeSound(): void {
        console.log(`${this.name} says: Meow!`);
    }
}

const dog = new Dog('Buddy');
const cat = new Cat('Whiskers');

dog.makeSound();
dog.move();

cat.makeSound();
cat.move();     

Output:

Buddy says: Woof!
Buddy is moving.
Whiskers says: Meow!
Whiskers is moving.

Advanced Usage with Abstract Methods

This TypeScript code defines an abstract Shape class with abstract methods area() and perimeter() , and a describe() method to log these values. The Circle and Rectangle classes extend Shape , implementing their respective area and perimeter calculations based on given dimensions (radius for circle, width and height for rectangle). Instances of Circle and Rectangle are created, and their describe() method outputs calculated area and perimeter values.

Example: This example shows the use of abstract class and the abstract function.

JavaScript
abstract class Shape {
    abstract area(): number;
    abstract perimeter(): number;

    describe(): void {
        console.log(`Area: ${this.area()}, 
        Perimeter: ${this.perimeter()}`);
    }
}

class Circle extends Shape {
    constructor(private radius: number) {
        super();
    }

    area(): number {
        return Math.PI * this.radius * this.radius;
    }

    perimeter(): number {
        return 2 * Math.PI * this.radius;
    }
}

class Rectangle extends Shape {
    constructor(private width: number, private height: number) {
        super();
    }

    area(): number {
        return this.width * this.height;
    }

    perimeter(): number {
        return 2 * (this.width + this.height);
    }
}

const circle = new Circle(5);
const rectangle = new Rectangle(10, 20);

circle.describe();
rectangle.describe(); 

Output:

Area: 78.53981633974483, Perimeter: 31.41592653589793
Area: 200, Perimeter: 60

Benefits of Using Abstract Classes

  • Code Reusability: Abstract classes allow you to define methods that can be shared across multiple derived classes, reducing code duplication.
  • Encapsulation: Abstract classes can hide the implementation details while exposing only the necessary methods that derived classes need to implement.
  • Polymorphism: Abstract classes enable polymorphism, allowing you to write more flexible and extensible code.

Next Article
Article Tags :

Similar Reads