How to implement Inheritance in Typescript ?
Last Updated :
14 Jul, 2021
Inheritance is a major pillar of object-oriented programming languages and inheritance is used to create a new class from the existing one. The newly created class is known as the Derived class and the class from which the derived class inherited is known as the Base class. An inherited derived class acquires the properties and behaviors of the base class. TypeScript supports single inheritance and multilevel inheritance. We can not implement hybrid and multiple inheritances using TypeScript. The inheritance uses class-based inheritance and it can be implemented using extends keywords in typescript. In this article, we will see how inheritance is implemented in TypeScript.
Syntax:
class baseClassName {
}
class derivedClassName extends baseClassName {
}
Single Inheritance in TypeScript: In single inheritance, the properties and behaviour of the base class can be inherited into at most one derived class. It used to add new functionality to the already implemented class.
Example: In this example, we are creating a Person as a base class and using single inheritance implementing Teacher as a Derived class.
JavaScript
// Base class
class Person {
Name: string;
constructor(name: string) {
this.Name = name;
}
}
// Deriving Teacher class
class Teacher extends Person {
Payment: number;
constructor(name: string, payment: number) {
super(name);
this.Payment = payment;
}
display(): void {
console.log("Teacher's Name: " + this.Name);
console.log("Teacher's Payment " + this.Payment);
}
}
// Create Object
let obj = new Teacher("GeeksforGeeks", 8500000);
obj.display();
Output:
Teacher's Name: GeeksforGeeks
Teacher's Payment 8500000
2.Multilevel Inheritance:
In multilevel inheritance, the derived class acts as the base class for another derived class. The newly created derived class acquires the properties and behavior of other base classes.
Example: In this example, we are creating a Vehicle as a base class and a Car as a subclass which deriving the new class called carModel and the class carModel acquires all features of the Vehicle as well as Car class.
JavaScript
// Base class
class Vehicle {
Type(): void {
console.log("Car");
}
}
// Inherites from Vehicle
class Car extends Vehicle {
Brand(): void {
console.log("ABC");
}
}
// Inherites all properties of
// Vehicle and Car class
class carModel extends Car {
Model(): void {
console.log("ABC2021");
}
}
// Object creation
let obj = new carModel();
obj.Type();
obj.Brand();
obj.Model();
Output:
Car
ABC
ABC2021
Similar Reads
How to implement inheritance in ES6 ? In this article, we will know how inheritance is implemented in es6. The ES6 JavaScript supports Object-Oriented programming components such as Object, Class and Methods. Further in Classes we can implement inheritance to make child inherits all methods of Parent Class. This can be done using the ex
2 min read
How to implement Type narrowing in TypeScript? Type narrowing in TypeScript refers to refining the type of a variable within a conditional block based on runtime checks. This is achieved through techniques like typeof guards, instance checks, or property existence checks, enabling more precise typing and avoiding type errors in subsequent code e
2 min read
How to Implement Recursive Generics in TypeScript ? In TypeScript, Recursive generics let you use generic types that refer to themselves inside their definition. This is helpful when we are working with nested or hierarchical Data Structures or Algorithms. Using this we can create flexible and reusable code for managing complex Data Structures. There
3 min read
How to implement inheritance in Node.js bindings? Node.js serves as a robust runtime environment, empowering developers to construct server-side applications that are both scalable and efficient. One of its key features is the ability to bind to C or C++ libraries, providing a bridge between JavaScript and lower-level languages. In this article, we
3 min read
TypeScript Inheritance Inheritance is a fundamental concept in object-oriented programming (OOP). It allows one class to inherit properties and methods from another class. The class that inherits is called the child class, and the class whose properties and methods are inherited is called the parent class. Inheritance ena
3 min read
How to import a module in Typescript ? Before starting with importing modules, first of all, we need to know the basics of modules in TypeScript. We know that JavaScript has come with the concept of modules from the ES6 version in 2015 and by 2020 had broad support in most web browsers and JavaScript runtimes. TypeScript also shares the
5 min read