Lesson 50 of 60 – Inheritance
83%

Inheritance in TypeScript

Inheritance allows one class to use the properties and methods of another class. It helps us reuse existing code and create relationships between classes.

Note: TypeScript uses the extends keyword to inherit from another class. The super() keyword is used to call the parent class constructor.

1. What is Inheritance?

Inheritance is an object-oriented programming concept where one class receives properties and methods from another class.

class Animal {
    eat(): void {
        console.log("Animal is eating");
    }
}

class Dog extends Animal {
}

2. Parent Class

The class whose properties and methods are inherited is called the parent class, base class, or superclass.

class Animal {
    eat(): void {
        console.log("Eating");
    }
}

3. Child Class

The class that inherits from another class is called the child class, derived class, or subclass.

class Dog extends Animal {
}

4. extends Keyword

The extends keyword is used to create inheritance between classes.

class Animal {
}

class Dog extends Animal {
}

5. Inheriting a Method

class Animal {
    eat(): void {
        console.log("Animal is eating");
    }
}

class Dog extends Animal {
}

const dog = new Dog();

dog.eat();

The Dog class can use the eat() method inherited from Animal.

6. Inheriting Properties

class Person {
    name: string = "Rahul";
}

class Student extends Person {
}

const student = new Student();

console.log(student.name);

7. Parent and Child Class Example

class Person {
    name: string;

    constructor(name: string) {
        this.name = name;
    }
}

class Student extends Person {
}

const student = new Student("Amit");

console.log(student.name);

8. Calling the Parent Constructor

A child class constructor can call the parent class constructor using super().

class Person {
    constructor(public name: string) {
    }
}

class Student extends Person {
    constructor(name: string) {
        super(name);
    }
}

9. What is super()?

super() calls the constructor of the parent class.

class Person {
    constructor(public name: string) {
    }
}

class Student extends Person {
    constructor(name: string) {
        super(name);
    }
}

10. super() Must Be Called Before this

In a derived class constructor, super() must be called before accessing this.

class Student extends Person {
    constructor(name: string) {
        super(name);

        console.log(this.name);
    }
}

11. Adding a New Property in Child Class

class Person {
    constructor(public name: string) {
    }
}

class Student extends Person {
    course: string;

    constructor(name: string, course: string) {
        super(name);
        this.course = course;
    }
}

12. Child Class with Its Own Method

class Animal {
    eat(): void {
        console.log("Eating");
    }
}

class Dog extends Animal {
    bark(): void {
        console.log("Barking");
    }
}

const dog = new Dog();

dog.eat();
dog.bark();

13. Parent Method and Child Method

A child class can have its own methods while also using methods inherited from the parent.

class Vehicle {
    start(): void {
        console.log("Vehicle started");
    }
}

class Car extends Vehicle {
    drive(): void {
        console.log("Car is driving");
    }
}

14. Method Overriding

A child class can provide its own implementation of a method inherited from the parent class.

class Animal {
    sound(): void {
        console.log("Animal sound");
    }
}

class Dog extends Animal {
    sound(): void {
        console.log("Dog barks");
    }
}

15. Calling an Overridden Method

const dog = new Dog();

dog.sound();

The child implementation is executed when the method is overridden.

16. Calling the Parent Method with super

The super keyword can also be used to call a parent class method.

class Animal {
    sound(): void {
        console.log("Animal sound");
    }
}

class Dog extends Animal {
    sound(): void {
        super.sound();
        console.log("Dog barks");
    }
}

17. protected Members and Inheritance

A protected member can be accessed inside the parent class and derived classes.

class Person {
    protected name: string;

    constructor(name: string) {
        this.name = name;
    }
}

class Student extends Person {
    showName(): void {
        console.log(this.name);
    }
}

18. private Members and Inheritance

A private member belongs only to the class where it is declared and cannot be directly accessed by a derived class.

class Person {
    private age: number = 20;
}

class Student extends Person {
    // Cannot directly access age
}

19. public Members and Inheritance

class Person {
    public name: string = "Rahul";
}

class Student extends Person {
    showName(): void {
        console.log(this.name);
    }
}

const student = new Student();

console.log(student.name);

20. Multilevel Inheritance

A class can inherit from another child class, creating multiple levels of inheritance.

class Animal {
    eat(): void {
        console.log("Eating");
    }
}

class Dog extends Animal {
    bark(): void {
        console.log("Barking");
    }
}

class Puppy extends Dog {
    play(): void {
        console.log("Playing");
    }
}

21. Using Multilevel Inheritance

const puppy = new Puppy();

puppy.eat();
puppy.bark();
puppy.play();

The Puppy object can use methods inherited through both parent levels.

22. Inheritance with Constructor Parameters

class Person {
    constructor(public name: string) {
    }
}

class Student extends Person {
    constructor(
        name: string,
        public course: string
    ) {
        super(name);
    }
}

const student = new Student(
    "Priya",
    "React Native"
);

23. Inheritance with Methods

class Person {
    constructor(public name: string) {
    }

    showName(): void {
        console.log(this.name);
    }
}

class Student extends Person {
    showCourse(): void {
        console.log("TypeScript");
    }
}

const student = new Student("Amit");

student.showName();
student.showCourse();

24. Practical Employee Inheritance

class Employee {
    constructor(
        public name: string,
        public salary: number
    ) {
    }

    showEmployee(): void {
        console.log(this.name);
        console.log(this.salary);
    }
}

class Manager extends Employee {
    constructor(
        name: string,
        salary: number,
        public department: string
    ) {
        super(name, salary);
    }

    showDepartment(): void {
        console.log(this.department);
    }
}

const manager = new Manager(
    "Rahul",
    50000,
    "IT"
);

manager.showEmployee();
manager.showDepartment();

25. Practical Student Inheritance

class Person {
    constructor(public name: string) {
    }

    showName(): void {
        console.log("Name:", this.name);
    }
}

class Student extends Person {
    constructor(
        name: string,
        public course: string
    ) {
        super(name);
    }

    showCourse(): void {
        console.log("Course:", this.course);
    }
}

const student = new Student(
    "Anita",
    "React Native"
);

student.showName();
student.showCourse();

26. Advantages of Inheritance

  • Promotes code reuse.
  • Reduces duplicate code.
  • Creates relationships between classes.
  • Makes large programs easier to organize.
  • Allows child classes to extend existing functionality.
  • Supports method overriding.

27. Common Inheritance Mistakes

  • Forgetting the extends keyword.
  • Forgetting super() in a derived constructor.
  • Using this before calling super().
  • Trying to access private parent members directly.
  • Using incorrect constructor parameters.
  • Overriding methods with incompatible implementations.

28. Inheritance with React Native

Understanding inheritance is useful when working with TypeScript classes in larger applications. It can help organize shared functionality between related classes.

class BaseService {
    log(message: string): void {
        console.log(message);
    }
}

class UserService extends BaseService {
    getUser(): void {
        this.log("Getting user");
    }
}

const service = new UserService();

service.getUser();

29. Inheritance Summary

  • extends creates inheritance between classes.
  • The parent class provides reusable functionality.
  • The child class can use inherited properties and methods.
  • super() calls the parent constructor.
  • super.method() can call a parent method.
  • Child classes can add their own properties and methods.
  • Methods can be overridden in child classes.
  • protected members are useful with inheritance.

30. Complete Inheritance Example

class Person {
    constructor(
        public name: string,
        public age: number
    ) {
    }

    showPerson(): void {
        console.log("Name:", this.name);
        console.log("Age:", this.age);
    }
}

class Student extends Person {
    constructor(
        name: string,
        age: number,
        public course: string
    ) {
        super(name, age);
    }

    showStudent(): void {
        console.log("Course:", this.course);
    }
}

const student = new Student(
    "Rahul",
    20,
    "React Native"
);

student.showPerson();
student.showStudent();

Here, Student inherits the properties and showPerson() method from Person. The student class also adds its own course property and showStudent() method.

📌 Key Points

  • Inheritance allows one class to reuse another class's functionality.
  • The extends keyword is used for inheritance.
  • The inherited class is called the parent or base class.
  • The inheriting class is called the child or derived class.
  • Child classes can use inherited public and protected members.
  • Private members cannot be directly accessed by child classes.
  • super() calls the parent class constructor.
  • super.method() can call a parent class method.
  • Child classes can add their own properties and methods.
  • Inheritance supports code reuse and method overriding.

🧠 Quick Quiz

Question: Which keyword is used to inherit one class from another class in TypeScript?