HomeToolsAbout a20k

Extends vs Implements

class Person { name: string; age: number; } class Child extends Person {} class Man implements Person {}

extends vs implements

extends is more for inheritance

  • one object acquires all the properties and behaviors of the parent object

implements is more for polymorphism

  • polymorphism is a provision of a single interface to entities of different types

extends extends a class

New class is a child

implements implements an interface

New class is the same shape, but not a child of the class

Class implements one or more interfaces

  • It enforces the class adheres to the structure defined by the interfaces it claims to implement

When a class implements an interface, it must provide implementations for all the methods and properties defined in that interface.

  • If the class fails to provide the required implementations, a compilation error will occur
interface Printable { print(): void; } class Book implements Printable { print() { console.log("Printing book..."); } } class Magazine implements Printable { print() { console.log("Printing magazine..."); } } const book = new Book(); book.print(); // Output: Printing book... const magazine = new Magazine(); magazine.print(); // Output: Printing magazine...
© VincentVanKoh