Course contentsShow
← TypeScript
Lesson 1054 of 1,79624. Classes in TypeScriptFree lesson

Abstract Constructors and Initialization

Why abstract classes can have constructors and how they run during subclass instantiation.

Abstract Constructors and Initialization

What you'll learn: How abstract classes handle constructors and what happens when you create instances of their subclasses.

The Core Concept

Even though you can't directly instantiate an abstract class (you can't write new AbstractClass()), abstract classes can have constructors. This might seem contradictory at first — why have a constructor for something you can't construct? The answer: the abstract class's constructor runs automatically when you instantiate any subclass, allowing it to set up shared initialization logic.

Think of it like a building foundation. You never build just a foundation by itself, but every house built on that foundation uses the same foundation-building process. The abstract class constructor is that foundation process.

How It Works

When you write new ConcreteClass():

  1. TypeScript creates the instance
  2. The abstract parent's constructor runs first
  3. Then the concrete child's constructor runs
  4. You get back a fully initialized concrete instance

The abstract constructor can initialize properties, validate invariants, or perform setup that every subclass needs — without duplicating that code in each child class.

Parameter Properties Work Too

Abstract constructors support parameter properties just like regular constructors. The abstract class can define protected or public parameter properties that become available to all subclasses.

This is particularly useful for shared state that every implementation needs but shouldn't be duplicated in each child constructor.

Common Pattern: Shared Initialization

The most common use case is when multiple concrete classes need the same foundational setup. The abstract constructor handles the common initialization, while each concrete class adds its specific behavior on top.

Key Takeaway: Abstract classes can have constructors that run during subclass instantiation, enabling shared initialization logic without code duplication across implementations.