Suggest an editImprove this articleRefine the answer for “How does a class implement an interface?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)When a class **implements an interface**, it commits to **implementing all the properties and methods** described in that interface, using the `implements` keyword. **Key point:** `implements` has no runtime effect, it only makes TypeScript check that the class's structure matches the interface, and a class can implement several interfaces at once, separated by commas.Shown above the full answer for quick recall.Answer (EN)Image## 1. How a class implements an interface When a class **implements an interface**, it commits to **implementing all the properties and methods** described in that interface. This is done using the `implements` keyword. --- ### Example 1: a simple interface implementation ```javascript interface IUser { name: string; age: number; greet(): void; } class User implements IUser { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } greet(): void { console.log(`Hello, my name is ${this.name}`); } } const user = new User("Tim", 25); user.greet(); // Hello, my name is Tim ``` Here: - The `IUser` interface defines a contract (the class must have `name`, `age`, `greet()`). - The `User` class **implements** that contract via `implements IUser`. If the class skips even one property or method, there is an error: ```javascript class BadUser implements IUser { name: string = "Alex"; // Error: Property 'age' is missing in type 'BadUser' } ``` --- ## 2. What the `implements` keyword does The `implements` keyword: - makes TypeScript **check that the class's structure matches** the interface; - has no effect at runtime, only on type checking; - guarantees that the class implements **all the interface's required fields and methods**. Important: TypeScript uses **structural typing**, meaning the interface's name doesn't matter, only that the shape matches. --- ### Example 2: a structural check ```javascript interface Logger { log(msg: string): void; } class ConsoleLogger implements Logger { log(msg: string) { console.log(msg); } } // Even if the interface isn't linked explicitly: const customLogger: Logger = new ConsoleLogger(); // OK, the structure matches ``` --- ## 3. Can you implement several interfaces? Yes! A class can **implement several interfaces at once**, listed with commas: ```javascript interface Printable { print(): void; } interface Serializable { serialize(): string; } class Document implements Printable, Serializable { print() { console.log("Printing document..."); } serialize() { return JSON.stringify({ content: "..." }); } } ``` > TypeScript checks that `Document` implements **all the methods of both interfaces**. --- ## 4. Interfaces can extend each other Sometimes it's convenient to create interfaces that inherit from one another: ```javascript interface Person { name: string; } interface Employee extends Person { position: string; } class Developer implements Employee { name: string; position: string; constructor(name: string, position: string) { this.name = name; this.position = position; } } ``` The `Developer` class implements `Employee`, which means it **automatically must contain** everything that's in both `Person` and `Employee`. --- ## 5. Combining with abstract classes You can use interfaces **together with abstract classes**: the interface sets the shape, the abstract class provides a base implementation: ```javascript interface Movable { move(distance: number): void; } abstract class Vehicle implements Movable { abstract move(distance: number): void; start() { console.log("Engine started"); } } class Car extends Vehicle { move(distance: number): void { console.log(`Driving ${distance} km`); } } ``` --- ## 6. Interfaces and access modifiers Interfaces describe **only a class's public interface**. They **cannot require private or protected** members: ```javascript interface IUser { name: string; getName(): string; } class User implements IUser { constructor(public name: string) {} getName() { return this.name; } private secret() {} // allowed, but not part of the interface } ``` > Everything described in the interface becomes **part of the class's public API**. --- ## 7. An interface for a class's static members Interfaces **cannot directly describe static fields and methods**. But this can be done indirectly, through the constructor's type: ```javascript interface IUserConstructor { new (name: string): IUserInstance; } interface IUserInstance { name: string; greet(): void; } function createUser(Ctor: IUserConstructor) { return new Ctor("Tim"); } class User implements IUserInstance { constructor(public name: string) {} greet() { console.log(`Hi, ${this.name}`); } } createUser(User); // OK ``` --- ## Summary | Question | Answer | |---|---| | **How does a class implement an interface?** | Via the `implements` keyword | | **What does** `implements` **do?** | Checks that the class matches the interface's structure | | **Can you implement several interfaces?** | Yes, separated by commas | | **Can you implement only some of the methods?** | No, the compiler raises an error | | **Does it work at runtime?** | No, it is only a type check | | **Can it be combined with inheritance (**`extends`**)?** | Yes: `class A extends B implements C, D` |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.