Method overriding

Sometimes, we will need a child class to provide a specific implementation of a method that is already provided by its parent class. We can use the reserved keyword super for this purpose.

We are going to use, once more, the Person and Teacher classes declared during the Inheritance section in this chapter.

Imagine that we want to add a new attribute to list the teacher's subjects, and we want to be able to initialize this attribute through the teacher constructor. We are going to use the super keyword to explicitly reference the parent class constructor inside the child class constructor:

class Person { public constructor( public name: string, public surname: string, public email: string ) {} public greet() { console.log("Hi!"); ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.