Skip to content
typescript questions

TypeScript · 6 of 10middle~1 min

What is the difference between extends and implements?

#oop#core

extends vs implements

extends - inheritance

A class inherits implementation (properties and methods) from a parent class.

ts
class Animal { constructor(public name: string) {} speak(): string { return `${this.name} makes a sound`; } } class Dog extends Animal { speak(): string { return `${this.name} barks`; } }

implements - contract

A class promises to satisfy a type shape - it must provide its own implementation.

ts
interface Serializable { serialize(): string; deserialize(data: string): void; } class User implements Serializable { serialize(): string { return JSON.stringify(this); } deserialize(data: string): void { Object.assign(this, JSON.parse(data)); } }

Using both together

ts
class AdminUser extends User implements Serializable, Loggable { // inherits from User, satisfies both interfaces }

Key difference

extendsimplements
Inherits implementationYesNo
MultipleNo (one class)Yes (many interfaces)
Used withA class (or an interface extending an interface)Interfaces and object types

implements with an interface is a great way to enforce a contract across unrelated classes.

updated

did you know it?

related questions