Extends vs implements in TypScript

typScript advance concepts extends vs implements

Tags: typscript, ts, extends, vs, implements, oop, object-oriented-programming

Extends VS implements

  • [ "extends" ]: The new class is a child which will inherit all the properties and methods of the class it extends from (the parent class), in simpler terms, it will take all the properties and methods from its parent class so you don't have to implement this yourself, It can override some or all of them and implement new ones, but the parent stuff is already included.

  • [ "implements" ]: The new class can be treated as the same "shape", not as a child, so it will not inherit any of the properties or methods of the class which it implements from, but still, you need to implement them, regardless of having different parent than the one it implements from.

This is just an example for all of you who are game developers criticize me to your heart content in the comment section :)

1class Character { 2 name: string; 3 type: string; 4 5 constructor(name: string, type: string) { 6 this.name = name; 7 this.type = type; 8 } 9 10 walk = (): void => { 11 console.log(`${this.name} walking...`); 12 }; 13} 14 15// NPC will inherit all the properties 16// and methods of the Character class 17class NPC extends Character { 18 constructor( 19 name: string, 20 type = 'NPC' // Making the default 'NPC' 21 ) { 22 super(name, type); 23 } 24} 25 26type PlayerOccupation = 27 | 'archer' 28 | 'knight' 29 | 'beast_tamer' 30 | 'wizard' 31 | 'assassin'; 32type PlayerType = 'noob' | 'experienced' | 'veteran' | 'legendry'; 33// Player has to implements at least all the properties 34// and methods of the Character class 35class Player implements Character { 36 name: string; 37 type: PlayerType; 38 mainOccupation: PlayerOccupation; // extra property that's not in the Character class 39 secondaryOccupation: PlayerOccupation; // extra property that's not in the Character class 40 41 constructor( 42 name: string, 43 type: PlayerType, 44 mainOccupation: PlayerOccupation, 45 secondaryOccupation: PlayerOccupation 46 ) { 47 this.name = name; 48 this.type = type; 49 this.mainOccupation = mainOccupation; 50 this.secondaryOccupation = secondaryOccupation; 51 } 52 53 walk = (): void => { 54 console.log(`${this.name} walking...`); 55 }; 56} 57 58const npc1 = new NPC('farmer'); 59 60const player = new Player('Adam', 'noob', 'archer', 'wizard');

[ "extends" ] to profit from inheritance (see wiki):

... Inheritance in most class-based object-oriented languages is a mechanism in which one object acquires all the properties and behaviors of the parent object. Inheritance allows programmers to: create classes that are built upon existing classes ...

[ "implements" ] will be more for polymorphism (see wiki):

... polymorphism is the provision of a single interface to entities of different types...

So, while

  • [ "extends" ] means it gets all from its parent.

  • [ "implements" ] in this case is almost like implementing an interface. A child object can pretend that it is a parent, but it doesn't get any implementation.

Resources and further reading