Suggest an editImprove this articleRefine the answer for “How does implements differ from extends in classes?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`extends`** inherits **implementation** and **state** from a parent class ("is a kind of", is-a), while **`implements`** requires a class to **match an interface (type)**, without inheriting any code ("matches the shape", conforms-to). **Key point:** `extends` allows only one class; `implements` allows several interfaces at once.Shown above the full answer for quick recall.Answer (EN)Image## Short answer | Keyword | What it does | Relation | |---|---|---| | `extends` | Inherits **implementation** and **state** from the parent class | "is a kind of" (is-a) | | `implements` | Requires 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. | Context | What `extends` does | |---|---| | On **classes** | Inherits implementation and state | | On **interfaces** | Merges (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 | Goal | What to choose | Why | |---|---|---| | You want to reuse existing code (methods, fields) | `extends` | You inherit the implementation | | You want to define a mandatory contract (API, interface) | `implements` | The class implements the details itself | | You want a shared shape for several interfaces | `extends` (between interfaces) | You merge the signatures | | You want to combine behavior + a contract | `extends` + `implements` | Typical for complex hierarchies | --- ## Summary | Keyword | For whom | What it does | Inherits implementation? | Can you use several? | |---|---|---|---|---| | **extends** | classes / interfaces | Inherits behavior (or signatures) | Yes (for classes) | Only one class | | **implements** | classes only | Checks conformance to an interface | No | Yes | --- **In simple terms:** > `extends` means "inherit everything it has". > `implements` means "do the same thing in shape, but with your own hands".For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.