EasyClasses & OOP📖 Theory Question

What is the difference between implements and extends in TypeScript?

💡

Hint

extends = inherit implementation (IS-A); implements = satisfy a contract (CAN-DO); class can implement multiple interfaces

Full Answer

extends — inherits from a parent class or interface. Gets all properties, methods, and constructor logic.

implements — declares that a class satisfies an interface contract, but does NOT inherit implementation.

// Interface contract
interface Serializable {
  serialize(): string;
}

interface Loggable {
  log(): void;
}

// extends — inherits implementation
class Animal {
  constructor(public name: string) {}
  breathe() { return 'breathing'; }
}

class Dog extends Animal {
  // Inherits name, breathe() from Animal
  bark() { return 'woof'; }
}

// implements — satisfy contract without inheriting
class User implements Serializable, Loggable {
  constructor(public name: string, public email: string) {}

  // MUST implement all interface methods
  serialize() { return JSON.stringify({ name: this.name, email: this.email }); }
  log() { console.log(this.name); }
}

// Combining both
class AdminUser extends User implements Comparable<AdminUser> {
  constructor(name: string, email: string, public adminLevel: number) {
    super(name, email); // must call parent constructor
  }

  compareTo(other: AdminUser): number {
    return this.adminLevel - other.adminLevel;
  }
}

Interface implements itself (structural check):

// A class can implement an interface to get compile-time checking
// that it fulfills the contract — even without runtime cost
class Repository implements IRepository<User> {
  // TypeScript errors if any method is missing
}
💡 TypeScript is structurally typed — you don't NEED to write implements if the class already has the right shape. But writing it explicitly documents intent and gives clearer error messages when the contract isn't met.

More Classes & OOP Questions

EasyWhat are access modifiers in TypeScript — public, private, protected, readonly?EasyWhat are abstract classes in TypeScript and when should you use them?EasyHow do decorators work in TypeScript, and what are they used for?MediumWhat are static members and class fields in TypeScript?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint