Inheritance

One of the most fundamental OOP features is its capability to extend existing classes. This feature is known as inheritance and allows us to create a new class (child class) that inherits all the properties and methods from an existing class (parent class). Child classes can include additional properties and methods that are not available in the parent class.

We are going to use the Person class that we declared in the preceding section as the parent class of a child class named Teacher. We can extend the parent class (Person) by using the reserved keyword extends:

class Teacher extends Person { 
    public teach() { 
        console.log("Welcome to class!"); 
    } 
} 

The Teacher class will inherit all the attributes and methods from its parent ...

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.