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

Combining implements and extends

A class can extend another class and implement interfaces simultaneously.

Combining implements and extends

What you'll learn: How to have a class inherit from another class while simultaneously implementing one or more interfaces.

Understanding the Combination

In TypeScript, you're not forced to choose between inheritance and interface implementation—you can use both at once. A class can extend exactly one parent class (TypeScript supports single inheritance) while simultaneously implementing multiple interfaces using implements.

The syntax is straightforward: extends comes first, followed by implements:

class Parent {
  parentMethod() {
    console.log("From parent");
  }
}

interface Flyable {
  fly(): void;
}

class Bird extends Parent implements Flyable {
  fly() {
    console.log("Flying!");
  }
}

How It Works

When a class uses both:

  1. Inheritance (extends): The class receives all concrete implementations, properties, and methods from the parent class. This provides actual runtime behavior.

  2. Interface contract (implements): The class must fulfill the type contract defined by the interface(s). This is purely compile-time checking.

abstract class Animal {
  protected name: string;

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

  eat(): void {
    console.log(`${this.name} is eating`);
  }
}

interface Swimmable {
  swim(): void;
  readonly maxDepth: number;
}

class Dolphin extends Animal implements Swimmable {
  readonly maxDepth = 300;

  constructor(name: string) {
    super(name); // Must call parent constructor
  }

  swim(): void {
    console.log(`${this.name} swims up to ${this.maxDepth}m deep`);
  }
}

const flipper = new Dolphin("Flipper");
flipper.eat();  // Inherited from Animal
flipper.swim(); // Required by Swimmable

Multiple Interfaces with Inheritance

You can implement multiple interfaces while extending a class:

class Vehicle {
  private speed: number = 0;

  accelerate(): void {
    this.speed += 10;
  }
}

interface Electric {
  charge(): void;
  batteryLevel: number;
}

interface Autonomous {
  enableAutopilot(): void;
}

class Tesla extends Vehicle implements Electric, Autonomous {
  batteryLevel: number = 100;

  charge(): void {
    this.batteryLevel = 100;
  }

  enableAutopilot(): void {
    console.log("Autopilot engaged");
  }
}

When This Pattern Shines

This combination is powerful when you need:

  • Base behavior from a class hierarchy (shared implementation)
  • Capability contracts from interfaces (what the class can do)
  • Multiple contracts that don't fit in a linear inheritance chain

Think of extends as "what you are" (an Animal, a Vehicle) and implements as "what you can do" (Flyable, Swimmable, Electric).

Key Takeaway: A class can extend one parent class and implement multiple interfaces simultaneously—combine them to inherit concrete behavior while enforcing capability contracts.