1 2 3 4 5 6 7 8 |
class Vehicle { constructor(public color: string) { } protected honk(): void { console.log('beep'); } } |
If we were to construct our Car, we need to make sure it call the parent class’s constructor. We do this by using super, and passing in whatever required parameters is required. In our case, it is color: string.
We then declare new property wheels for Car.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Car extends Vehicle { // whenever instance is created, the constructor in parent is being called automatically constructor(public wheels: number, color: string) { super(color); } private drive(): void { console.log('vroooom'); } startDrivingProcess(): void { this.honk(); this.drive(); console.log('this ' + this.color + ' car drove away'); } } const car = new Car(3, 'black'); car.startDrivingProcess(); |
Lastly, make sure we put in the required constructor params for Car when we instantiate it.