How does a class implement an interface?
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
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 TimHere:
- The
IUserinterface defines a contract (the class must havename,age,greet()). - The
Userclass implements that contract viaimplements IUser.
If the class skips even one property or method, there is an error:
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
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 matches3. Can you implement several interfaces?
Yes! A class can implement several interfaces at once, listed with commas:
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
Documentimplements all the methods of both interfaces.
4. Interfaces can extend each other
Sometimes it's convenient to create interfaces that inherit from one another:
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:
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:
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:
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); // OKSummary
| 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 |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.