Table of contents
1.
Introduction
2.
Types of Inheritance
3.
Single Inheritance
4.
Hierarchical Inheritance
5.
Multilevel Inheritance
6.
FAQ’s
7.
Key Takeaways
Last Updated: Mar 27, 2024

Typescript-Type Inheritence

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Inheritance is a property of the Object-Oriented Programming Language(OOPS). The Inheritance property allows the user to fetch functions or variables from a class, called the parent or base class, to a different class, known as the derived or child class. We can further override the inherited properties in the derived class according to the needs.

Inheritance in Typescript is a little different from Inheritance in Javascript. Javascript uses the function and prototype-based Inheritance, whereas Typescript uses class-based Inheritance. We can achieve Inheritance using the extends keyword in Typescript.

A class in Typescript is basically a user-defined blueprint or prototype used for object creation. Syntax to create a class in Typescript looks like this,

class class_name{
//variables, functions, and methods
}

Types of Inheritance

Inheritance can be classified into five types,

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

In our case, we are only considered with singlemultilevel and hierarchical Inheritance as Typescript only supports these three types of Inheritance.

Single Inheritance

Single Inheritance is the least complex type of Inheritance. In this Inheritance, one class inherits the functionality of another class. There are only two classes involved, the base and derived class. Class A is the base class in the below-shown flowchart, and Class B is the derived class.

As mentioned above, Class A can be inherited in Class B using the extends keyword.

Let us look at an example of how to implement Single Inheritance,

class Pet{
    name: string; 
    constructor(name:string) {   
        this.name = name;  
   }   
}
class Dog extends Pet{
    display():void{
        console.log("name of my dog is : " + this.name);
    }
}
var obj = new Dog("Casper");
obj.display();

In this example, the class dog inherits the class Pet. We can directly use a variable declared in the Pet class in our derived Dog class.

Know about Single Inheritance in Java in detail.

Hierarchical Inheritance

In Hierarchical Inheritance, two or more classes inherit the properties of one class.

In hierarchical Inheritance, there are two or more base classes but only one derived class.

For example,

class Pet{
    name:string; 
    constructor(name:string) {   
        this.name = name;  
   }   
}
class Dog extends Pet{
    display():void{
        console.log("name of my dog is : " + this.name);
    }
}
class Fish extends Pet{
    display():void{
        console.log("name of my fish is : " + this.name);
    }
}
var obj = new Dog("Casper");
obj.display();
var obj = new Fish("Goldie");
obj.display();

In this example, we have considered the program from the previous model and added one more derived class named Fish, and we inherited the Pet class in it.

Multilevel Inheritance

In Multilevel Inheritance, the base class becomes the derived class for some other class. Let us understand it using the below-shown flowchart.

In the above flowchart, Class B is the base class for Class C, whereas Class B is the derived class of Class A.

For example,

class Pet{
    display_pet():void{
        console.log("This is the Pet class")
    }
}
class Dog extends Pet{
    display_dog():void{
        console.log("This is the Dog class");
    }
}
class Eat extends Dog{
    display_eat():void{
        console.log("Dog is eating");
    }
}
var obj = new Eat();
obj.display_pet();
obj.display_dog();
obj.display_eat();

In the above example, we have used an object of the Eat Class to call the functions of Dog and Pet Class.

FAQ’s

  1. Why is Inheritance used?
    Inheritance is generally used to achieve either of the two following purposes:
    Inheritance is sometimes used for method overriding.
    Inheritance is also used for code reusability.
     
  2. What is Hybrid Inheritance?
    Hybrid Inheritance can be viewed as a mixture of two or more different types of Inheritance. It cannot be implemented using Typescript.

Key Takeaways

This Blog covered all the necessary points about Inheritance in Typescript, discussing in-depth its functionality and different methods of the appliance of Inheritance. And also, the different types of Inheritance in Typescript.

Don't stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

 

Recommended Readings:

Live masterclass