TypeScript Tutorial

Abstract Classes

Defining base classes, abstract methods, and inheritance

What are Abstract Classes?

An Abstract Class is a base class that cannot be instantiated directly. It serves as a "blueprint" for other classes to inherit from. It can contain both implemented methods and Abstract Methods (methods that have no body and MUST be implemented by subclasses).
typescript
abstract class Shape {
    constructor(public color: string) {}

    abstract getArea(): number; // No implementation!

    printInfo() {
        console.log(`A ${this.color} shape with area ${this.getArea()}`);
    }
}

Implementing Subclasses

Wait! You use the extends keyword to inherit from an abstract class. If you don't implement all the abstract methods, the compiler will error.
typescript
class Circle extends Shape {
    constructor(color: string, private radius: number) {
        super(color);
    }

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

Abstract vs. Interfaces

Wait! When should you use an abstract class instead of an interface?
  • Interface: Describes ONLY the shape (behavior) of an object. It cannot contain implementation code or logic.
  • Abstract Class: Can contain common logic (implementation) that all subclasses should share.
FeatureInterfaceAbstract Class
LogicNone (Definition only)Yes (Shared methods)
InstantiateNoNo
MultiCan implement manyCan only inherit one
Access Mod.No (All public)Yes (private, protected)

Summary

TermDescription
abstractKeyword to define the class or method
extendsKeyword to inherit from the base class
super()Calling the base class constructor
BlueprintAbstract classes provide structure for related types
SharedGreat for putting common logic in one place
EnforcedSubclasses MUST implement abstract methods
Best ForComplex class hierarchies (e.g., UI components, DB models)
LogicCombines the power of interfaces and regular classes
SafetyPrevents creating "generic" objects that aren't fully formed
ModernPowering large-scale OOP applications in TypeScript
IdentityAn abstract class is a REAL class (it exists in JS output)
HierarchyUse inheritance for "Is-A" relationships