ProgramGuru

Abstract Classes

Understand Abstract Classes

An abstract class can define shared implementation while requiring subclasses to implement specified abstract members.

What you will learn: the purpose of abstract classes, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how abstract classes works and where its type rules apply.
  • Predict the exact output of the abstract classes example.
  • Diagnose a common issue: Do not instantiate new Shape() or omit its abstract methods in a concrete subclass; strict TypeScript will flag both.

Build the mental model

You cannot instantiate an abstract class directly in checked TypeScript. Abstract contracts are erased from the emitted JavaScript; use runtime checks when untrusted code must be prevented from violating an invariant.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Abstract Classes: explained visuallyShape: abstract area → Square: area() → describe() worksShape: abstract areaSquare: area()describe() worksFollow the values through the operation
Abstract Classes: Shape: abstract area → Square: area() → describe() works.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

abstract class Shape {
  abstract area(): number;
  describe(): string { return `Area: ${this.area()}`; }
}
class Square extends Shape {
  constructor(public side: number) { super(); }
  area(): number { return this.side * this.side; }
}
const square: Shape = new Square(5);
console.log(square.describe());

Verified output

Area: 25

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

Square implements the required area method and calls super in its constructor. The inherited describe method calls area using the actual Square instance and prints Area: 25.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not instantiate new Shape() or omit its abstract methods in a concrete subclass; strict TypeScript will flag both.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Create a Rectangle subclass implementing area while reusing Shape.describe.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What obligation does abstract area(): number impose?
Choose one answer. You can try again.

Answer and explanation

Each concrete subclass must supply a compatible area implementation.

An abstract class can define shared implementation while requiring subclasses to implement specified abstract members.

Review the key idea

Abstract Classes: An abstract class can define shared implementation while requiring subclasses to implement specified abstract members.

Square implements the required area method and calls super in its constructor. The inherited describe method calls area using the actual Square instance and prints Area: 25.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Implementing Interfaces →