Lesson 51 of 60 – Abstract Classes
85%

Abstract Classes in TypeScript

An abstract class is a special class that is designed to be inherited by other classes. It can contain normal methods, properties, and abstract methods.

Note: An abstract class cannot be directly instantiated. It is mainly used as a base class for other classes.

1. What is an Abstract Class?

An abstract class is a class that provides a common structure for other classes.

abstract class Animal {
}

The abstract keyword is used before the class keyword.

2. abstract Keyword

The abstract keyword is used to define an abstract class or an abstract method.

abstract class Person {
}

3. Abstract Classes Cannot Be Instantiated

You cannot create an object directly from an abstract class.

abstract class Animal {
}

// Error
// const animal = new Animal();

4. Why Use Abstract Classes?

Abstract classes are useful when several classes share common functionality but also need their own specific implementations.

  • Define a common structure.
  • Share common code.
  • Force child classes to implement specific methods.
  • Improve code organization.

5. Abstract Method

An abstract method is declared without a method body. The child class must provide its implementation.

abstract class Animal {
    abstract makeSound(): void;
}

6. Abstract Method Syntax

An abstract method contains the abstract keyword, its name, parameters if required, and its return type.

abstract showDetails(): void;

Notice that there is no method body.

7. Implementing an Abstract Method

abstract class Animal {
    abstract makeSound(): void;
}

class Dog extends Animal {
    makeSound(): void {
        console.log("Dog barks");
    }
}

8. Creating an Object of the Child Class

const dog = new Dog();

dog.makeSound();

We create an object of the concrete child class rather than the abstract parent class.

9. Child Class Must Implement Abstract Methods

A non-abstract child class must provide implementations for the abstract methods inherited from its parent.

abstract class Animal {
    abstract eat(): void;
}

class Dog extends Animal {
    eat(): void {
        console.log("Dog eats");
    }
}

10. Abstract Class with a Normal Method

An abstract class can contain normal methods with complete implementations.

abstract class Animal {

    move(): void {
        console.log("Animal is moving");
    }

    abstract makeSound(): void;
}

11. Using Normal and Abstract Methods

abstract class Animal {

    move(): void {
        console.log("Animal is moving");
    }

    abstract makeSound(): void;
}

class Dog extends Animal {

    makeSound(): void {
        console.log("Dog barks");
    }
}

const dog = new Dog();

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

12. Abstract Properties

An abstract class can also declare abstract properties.

abstract class Animal {
    abstract name: string;
}

The child class must provide the property.

13. Implementing an Abstract Property

abstract class Animal {
    abstract name: string;
}

class Dog extends Animal {
    name: string = "Dog";
}

const dog = new Dog();

console.log(dog.name);

14. Abstract Class with Constructor

An abstract class can have a constructor. The constructor is used when a child class is created.

abstract class Person {
    constructor(public name: string) {
    }
}

15. Calling Abstract Parent Constructor

abstract class Person {
    constructor(public name: string) {
    }
}

class Student extends Person {
    constructor(name: string) {
        super(name);
    }
}

const student = new Student("Rahul");

console.log(student.name);

16. Abstract Class with Multiple Methods

abstract class Employee {

    abstract calculateSalary(): number;

    abstract showRole(): void;

    displayCompany(): void {
        console.log("ABC Company");
    }
}

17. Implementing Multiple Abstract Methods

abstract class Employee {

    abstract calculateSalary(): number;

    abstract showRole(): void;
}

class Manager extends Employee {

    calculateSalary(): number {
        return 50000;
    }

    showRole(): void {
        console.log("Manager");
    }
}

18. Abstract Class and Inheritance

Abstract classes are commonly used as base classes in inheritance.

abstract class Vehicle {
    abstract start(): void;
}

class Car extends Vehicle {
    start(): void {
        console.log("Car started");
    }
}

19. Abstract Class with protected Property

abstract class Person {
    protected name: string;

    constructor(name: string) {
        this.name = name;
    }

    abstract showDetails(): void;
}

class Student extends Person {

    showDetails(): void {
        console.log("Student:", this.name);
    }
}

20. Abstract Class and Method Overriding

The child class provides its own implementation of an abstract method.

abstract class Animal {
    abstract sound(): void;
}

class Cat extends Animal {

    sound(): void {
        console.log("Cat meows");
    }
}

21. Abstract Class with Shared Logic

An abstract class can provide common logic while leaving specific behavior to child classes.

abstract class Payment {

    process(): void {
        console.log("Processing payment");
    }

    abstract pay(): void;
}

class CardPayment extends Payment {

    pay(): void {
        console.log("Paid using card");
    }
}

22. Practical Shape Example

abstract class Shape {

    abstract area(): number;

    display(): void {
        console.log("This is a shape");
    }
}

class Rectangle extends Shape {

    constructor(
        private width: number,
        private height: number
    ) {
        super();
    }

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

const rectangle = new Rectangle(10, 5);

rectangle.display();

console.log(rectangle.area());

23. Abstract Class vs Normal Class

Feature Normal Class Abstract Class
Create Object Yes No
Normal Methods Yes Yes
Abstract Methods No Yes
Inheritance Yes Yes

24. Abstract Class vs Interface

Both abstract classes and interfaces can define structures that other classes must follow, but they are used differently.

  • Abstract classes can contain implemented methods.
  • Abstract classes can contain constructors.
  • Abstract classes can contain properties and state.
  • Interfaces primarily describe the shape of objects.
  • A class extends an abstract class using extends.

25. Practical Employee Example

abstract class Employee {

    constructor(
        public name: string
    ) {
    }

    showName(): void {
        console.log("Name:", this.name);
    }

    abstract calculateSalary(): number;
}

class Developer extends Employee {

    constructor(
        name: string,
        private salary: number
    ) {
        super(name);
    }

    calculateSalary(): number {
        return this.salary;
    }
}

const developer = new Developer(
    "Amit",
    40000
);

developer.showName();

console.log(
    developer.calculateSalary()
);

26. Advantages of Abstract Classes

  • Provide a common base for related classes.
  • Allow code reuse.
  • Force child classes to implement required methods.
  • Allow both abstract and normal methods.
  • Can contain constructors and properties.
  • Help organize large applications.

27. Common Abstract Class Mistakes

  • Trying to create an object directly from an abstract class.
  • Forgetting to implement an abstract method in a concrete child class.
  • Adding a method body to an abstract method declaration.
  • Forgetting super() in a derived constructor.
  • Using the wrong return type for an abstract method.
  • Confusing abstract classes with interfaces.

28. Abstract Classes in Application Design

Abstract classes can be useful when different services, components, or application objects share common behavior but need different implementations for some operations.

abstract class ApiService {

    log(): void {
        console.log("API service");
    }

    abstract fetchData(): void;
}

class UserService extends ApiService {

    fetchData(): void {
        console.log("Fetching users");
    }
}

29. Abstract Class Summary

  • Use abstract class to define a base class.
  • Abstract classes cannot be instantiated directly.
  • Use abstract for methods or properties that child classes must provide.
  • Abstract classes can contain normal methods.
  • Abstract classes can contain constructors.
  • Child classes inherit from abstract classes using extends.
  • Child constructors use super() to call the parent constructor.
  • Abstract classes are useful for code reuse and common structures.

30. Complete Abstract Class Example

abstract class Employee {

    constructor(
        public name: string,
        protected department: string
    ) {
    }

    showDetails(): void {
        console.log("Name:", this.name);
        console.log("Department:", this.department);
    }

    abstract calculateSalary(): number;

    abstract getRole(): string;
}

class Developer extends Employee {

    constructor(
        name: string,
        department: string,
        private salary: number
    ) {
        super(name, department);
    }

    calculateSalary(): number {
        return this.salary;
    }

    getRole(): string {
        return "Software Developer";
    }
}

const developer = new Developer(
    "Rahul",
    "IT",
    50000
);

developer.showDetails();

console.log(
    "Role:",
    developer.getRole()
);

console.log(
    "Salary:",
    developer.calculateSalary()
);

In this example, Employee provides common functionality and defines two abstract methods. Developer inherits from it and implements those methods.

📌 Key Points

  • An abstract class is designed to be inherited by other classes.
  • It is declared using the abstract keyword.
  • An abstract class cannot be instantiated directly.
  • Abstract classes can contain normal methods.
  • Abstract classes can contain abstract methods.
  • Abstract methods do not have a method body.
  • Concrete child classes must implement abstract methods.
  • Abstract classes can contain constructors and properties.
  • Child classes use extends to inherit from an abstract class.
  • super() is used to call the parent constructor.

🧠 Quick Quiz

Question: Which keyword is used to define an abstract class in TypeScript?