Skip to main content

How does implements differ from extends in classes?

Short answer

KeywordWhat it doesRelation
extendsInherits implementation and state from the parent class"is a kind of" (is-a)
implementsRequires a class to match an interface (type), but does not inherit code"matches the shape" (conforms-to)

1. extends - Inheriting behavior

A class with extends inherits the parent's fields and methods. It can override them and call them through super.

Example:

javascript
class Animal { move() { console.log("Animal moves"); } } class Dog extends Animal { bark() { console.log("Woof!"); } } const dog = new Dog(); dog.move(); // inherited from Animal dog.bark(); // its own method

Dog inherits the implementation from Animal. This means Dog already knows how to move, without rewriting the move method.


2. implements - Implementing a contract (interface)

A class with implements gets nothing "for free". It is simply required to implement all the properties and methods listed in the interface.

Example:

javascript
interface Logger { log(message: string): void; } class ConsoleLogger implements Logger { log(message: string) { console.log(message); } } const logger = new ConsoleLogger(); logger.log("Hello!"); // implemented by hand

Here Logger is a contract, and ConsoleLogger commits to implementing all its methods.


3. The main difference in one sentence

  • extends -> inherits implementation and state (code, fields, methods).
  • implements -> guarantees the presence of methods and properties (a contract), but you write the implementation yourself.

4. You can combine them

You can inherit from one class and implement several interfaces at the same time:

javascript
interface Swimmer { swim(): void; } interface Runner { run(): void; } class Animal { eat() {} } class Human extends Animal implements Swimmer, Runner { swim() { console.log("Swimming"); } run() { console.log("Running"); } } const person = new Human(); person.eat(); // from Animal person.swim(); // from Swimmer person.run(); // from Runner
  • extends allows only one (class),
  • implements allows many (interfaces).

5. extends for classes vs interfaces

TypeScript allows extends on interfaces too, but there it works a bit differently.

ContextWhat extends does
On classesInherits implementation and state
On interfacesMerges (inherits) signatures, creating a new type

Example:

javascript
interface A { a: number; } interface B extends A { b: string; } const obj: B = { a: 1, b: "hi" }; // merged properties

6. You cannot use implements with an interface inside an interface

Interfaces can extend each other (extends), but they cannot "implement", implements is meant only for classes.

javascript
// Error: interface X implements Y {}

7. Multiple inheritance (and why it is forbidden)

In TypeScript (as in most OOP languages, except C++) you cannot inherit from several classes at once, because this leads to implementation conflicts.

javascript
class A {} class B {} // Error class C extends A, B {}

But you can implement several interfaces:

javascript
interface X {} interface Y {} class C implements X, Y {}

8. A real example: a shared interface and a concrete implementation

javascript
interface Repository { save(data: object): void; findById(id: number): object; } class UserRepository implements Repository { save(data: object) { console.log("Saved", data); } findById(id: number) { return { id, name: "Alex" }; } }

UserRepository is required to implement the save and findById methods, but can implement them however it wants.


9. Using extends in classes with super

javascript
class Vehicle { constructor(public speed: number) {} move() { console.log(`Moving at ${this.speed} km/h`); } } class Car extends Vehicle { constructor(speed: number, public brand: string) { super(speed); // calls the Vehicle constructor } move() { super.move(); // calls the parent's method console.log(`${this.brand} is driving`); } } const car = new Car(100, "BMW"); car.move(); // Moving at 100 km/h // BMW is driving

extends means inheriting behavior plus the ability to override it.


10. When to use which

GoalWhat to chooseWhy
You want to reuse existing code (methods, fields)extendsYou inherit the implementation
You want to define a mandatory contract (API, interface)implementsThe class implements the details itself
You want a shared shape for several interfacesextends (between interfaces)You merge the signatures
You want to combine behavior + a contractextends + implementsTypical for complex hierarchies

Summary

KeywordFor whomWhat it doesInherits implementation?Can you use several?
extendsclasses / interfacesInherits behavior (or signatures)Yes (for classes)Only one class
implementsclasses onlyChecks conformance to an interfaceNoYes

In simple terms:

extends means "inherit everything it has". implements means "do the same thing in shape, but with your own hands".

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.