class Person { name: string; age: number; } class Child extends Person {} class Man implements Person {}
extends
vs implements
extends
is more for inheritance
implements
is more for polymorphism
extends
extends a classNew 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
When a class implements an interface, it must provide implementations for all the methods and properties defined in that interface.
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...