A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/typescript/what-are-abstract-classes-in-typescript/ below:

What are Abstract Classes in TypeScript?

What are Abstract Classes in TypeScript?

Last Updated : 23 Jan, 2025

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

JavaScript
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();

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 JavaScript
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();

Output

James
Abstract Class with Abstract Method JavaScript
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();

Output:

The area is 78.53981633974483.
Best Practices for Using Abstract Classes in TypeScript:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4