Open In App

What are Abstract Classes in TypeScript?

Last Updated : 23 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

An abstract class in TypeScript serves as a blueprint for other classes and cannot be instantiated directly.

  • It may include abstract methods without implementation, which must be defined in any subclass.
  • Additionally, it can contain concrete methods with implementations, properties, and other members.
abstract class Animal {
  abstract makeSound(): void;

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

class Dog extends Animal {
  makeSound(): void {
    console.log('Bark');
  }
}

const myDog = new Dog();
myDog.makeSound();
myDog.move();
  • The Animal class is declared as abstract, containing an abstract method makeSound() without implementation and a concrete method move() with implementation.
  • The Dog class extends Animal and provides an implementation for the makeSound() method.
  • An instance of Dog is created, and both makeSound() and move() methods are called, demonstrating the use of both abstract and concrete methods.

Output:

Bark
Moving...

Key Features of Abstract Classes

  1. Cannot Be Instantiated: It is impossible to instantiate abstract classes directly and any such attempt will fail at compilation.
  2. Abstract Methods: In this methods declared within the abstract class without any implementation and subclasses must provide specific implementation for them.
  3. Concrete Methods: Abstract classes can have fully implemented methods, which can be inherited and used by subclasses.
  4. Can Include Properties and Constructors: Abstract classes could have constructors as well as properties though they cannot be instantiated, thus, typically used to set up common logic or initialize shared properties for derived classes.
  5. Polymorphism: Polymorphism is provided by abstract classes which enable treating its children like instances of itself.

Applications of Abstract Classes

  1. Defining a Common Interface: Abstract classes define common interface for related classes and in this way there is a shared structure of all subclasses to make it easier to understand and maintain the codebase.
  2. Code Reuse: Common methods and properties that an abstract class contains can be inherited by child objects which reduce duplication of efforts during coding, it also makes your application easier to maintain.
  3. Enforcing Implementation: Subclasses must implement specific implementations through abstract methods in an abstract class, it is when certain methods have different implementations depending on various sub-classes.
  4. Encapsulation of Shared Logic: Changes can be managed centrally by containing them within abstract classes that are responsible for many similar sub-classes.

More Example of Abstract Classes in TypeScript

Abstract Class with Abstract Property

abstract class Person {
  abstract name: string;

  display(): void {
    console.log(this.name);
  }
}

class Employee extends Person {
  name: string;
  empCode: number;

  constructor(name: string, code: number) {
    super();
    this.name = name;
    this.empCode = code;
  }
}

const emp = new Employee('James', 100);
emp.display();
  • The Person abstract class declares an abstract property name and a concrete method display() that logs the name.
  • The Employee class extends Person, providing an implementation for the name property and adding an empCode property.
  • An instance of Employee is created with the name 'James' and code 100, and the display() method is called to output the name.

Output

James

Abstract Class with Abstract Method

abstract class Shape {
  abstract getArea(): number;

  printArea(): void {
    console.log(`The area is ${this.getArea()}.`);
  }
}

class Circle extends Shape {
  radius: number;

  constructor(radius: number) {
    super();
    this.radius = radius;
  }

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

const circle = new Circle(5);
circle.printArea();
  • The Shape abstract class includes an abstract method getArea() and a concrete method printArea() that prints the area.
  • The Circle class extends Shape, implementing the getArea() method to calculate the area of the circle.
  • An instance of Circle is created with a radius of 5, and the printArea() method is called to display the area.

Output:

The area is 78.53981633974483.

Best Practices for Using Abstract Classes in TypeScript:

  • Use Abstract Classes for Shared Functionality: When multiple related classes share common behavior, define an abstract class to encapsulate this shared functionality. This promotes code reuse and consistency.
  • Define Abstract Methods for Mandatory Implementations: If certain methods must be implemented by all subclasses, declare them as abstract in the base class. This enforces a contract for derived classes to provide specific implementations.
  • Avoid Instantiating Abstract Classes: Abstract classes are not meant to be instantiated directly. Ensure that only their subclasses are instantiated to maintain the intended design pattern.

What are Abstract Classes in TypeScript- FAQs

Can an abstract class have implemented methods?

Yes, abstract classes can include both abstract methods (without implementation) and concrete methods (with implementation).

What happens if a subclass does not implement all abstract methods?

The subclass must be declared as abstract itself, or it will result in a compilation error.

How do abstract classes differ from interfaces in TypeScript?

Abstract classes can provide implementations for some methods and can maintain state through fields, whereas interfaces only declare method signatures and properties without implementations.

Can abstract classes have constructors?

Yes, abstract classes can have constructors to initialize shared properties or perform setup tasks for subclasses.

Is it possible to create an instance of an abstract class?

No, abstract classes cannot be instantiated directly; they must be extended by a subclass that implements the abstract methods.


Next Article

Similar Reads

three90RightbarBannerImg