TypeScript Abstract Classes
Learn how abstract classes define a common structure for child classes using abstract properties and methods in TypeScript.
1. What is an Abstract Class?
An abstract class is a class declared using the
abstract keyword. It is designed to be extended
by other classes instead of being instantiated directly.
An abstract class can contain abstract methods without implementations as well as normal methods with implementations.
Basic Syntax
abstract class Animal {
abstract makeSound(): void;
eat(): void {
console.log("Animal is eating");
}
}
class Dog extends Animal {
makeSound(): void {
console.log("Dog is barking");
}
}
const dog1 = new Dog();
dog1.makeSound();
dog1.eat();
Expected Output
Dog is barking
Animal is eating
makeSound() because Dog is a concrete class.
2. Abstract Class Workflow Diagram
This diagram shows how an abstract class defines a common contract that child classes implement.
Abstract Class
Defines common behavior and abstract methods.
extends
A child class inherits the abstract class.
Implement Methods
The child class provides required implementations.
Create Object
Create an instance of the concrete child class.
new Animal() when Animal is abstract.
Create an object of a concrete class such as Dog instead.
3. Abstract Methods
An abstract method declares what a child class must implement without providing the method body in the abstract class. Abstract methods can be declared only within an abstract class.
Example
abstract class Payment {
abstract pay(amount: number): void;
}
class UpiPayment extends Payment {
pay(amount: number): void {
console.log("Paid using UPI: " + amount);
}
}
const payment1 = new UpiPayment();
payment1.pay(1500);
Expected Output
Paid using UPI: 1500
Explanation
Payment
Declares the abstract pay() method.
UpiPayment
Implements pay() with its own behavior.
Payment Object
Calls the implemented method through UpiPayment.
4. Abstract and Concrete Methods
An abstract class can contain both abstract and concrete methods. A concrete method already has an implementation that child classes can inherit and use.
Example
abstract class Employee {
abstract calculateSalary(): number;
showCompany(): void {
console.log("Company: CIIT");
}
}
class FullTimeEmployee extends Employee {
calculateSalary(): number {
return 40000;
}
}
const employee1 = new FullTimeEmployee();
employee1.showCompany();
console.log("Salary: " + employee1.calculateSalary());
Expected Output
Company: CIIT
Salary: 40000
Difference
| Abstract Method | Concrete Method |
|---|---|
| Declares a method without a body. | Contains an implementation. |
| Must be implemented by a concrete child class. | Can be inherited and used directly. |
Example: abstract calculateSalary(): number; |
Example: showCompany(): void { ... } |
5. Practical Example: Calculating Shape Areas
Suppose an application calculates the area of different shapes. Every shape has an area, but the calculation depends on the shape. An abstract class can define the common method while each child class implements its own formula.
Example
abstract class Shape {
abstract calculateArea(): number;
displayArea(): void {
console.log("Area: " + this.calculateArea());
}
}
class Rectangle extends Shape {
constructor(
private width: number,
private height: number
) {
super();
}
calculateArea(): number {
return this.width * this.height;
}
}
class Circle extends Shape {
constructor(private radius: number) {
super();
}
calculateArea(): number {
return Math.PI * this.radius * this.radius;
}
}
const rectangle = new Rectangle(10, 5);
rectangle.displayArea();
const circle = new Circle(7);
circle.displayArea();
Expected Output
Area: 50
Area: 153.93804002589985
The rectangle calculates area as width multiplied by height. The circle uses the formula π multiplied by radius squared.
Visual Representation
Shape
Abstract base class with calculateArea().
Rectangle
Width: 10
Height: 5
Area: 50
Circle
Radius: 7
Area: Approximately 153.94
6. Important Rules of Abstract Classes
| Rule | Explanation |
|---|---|
| Declare with abstract | Use the abstract keyword before the class or method declaration. |
| No direct instance | An abstract class cannot be instantiated directly. |
| Extend the class | A child class uses extends to inherit from it. |
| Implement abstract members | A concrete child class must implement inherited abstract members. |
| Concrete methods allowed | An abstract class can include methods with implementations. |
| Constructor support | An abstract class can have a constructor that a child class calls using super(). |
7. Advantages of Abstract Classes
Common Structure
Defines common properties and methods for related classes.
Enforced Implementation
Requires concrete subclasses to implement abstract methods.
Code Reusability
Shares common concrete methods through a base class.
Maintainability
Keeps related classes consistent and easier to maintain.
Summary
Abstract classes provide a common blueprint for related classes. They can contain abstract methods that subclasses must implement and concrete methods that subclasses can reuse. Abstract classes cannot be instantiated directly.
Next, learn static members and how properties and methods can belong to a class rather than individual object instances.