TypeScript Inheritance
Learn how one class can inherit properties and methods from another class using the extends keyword in TypeScript.
1. What is Inheritance?
Inheritance is an Object-Oriented Programming concept in which one class derives functionality from another class.
The class whose functionality is inherited is called the parent class or base class. The class that inherits it is called the child class or derived class.
TypeScript uses the extends keyword to establish
inheritance between classes.
Basic Syntax
class Parent {
// Parent class properties and methods
}
class Child extends Parent {
// Child class properties and methods
}
extends keyword allows the child class to inherit
accessible members from the parent class.
2. Inheritance Workflow Diagram
Understand how a child class inherits functionality from a parent class and uses it through an object.
Parent Class
Defines common properties and methods.
extends
Connects the child class to the parent class.
Child Class
Inherits accessible members and can add new functionality.
Object
Uses inherited and child-defined methods.
3. Basic Inheritance Example
In this example, the Dog class inherits the
eat() method from the Animal class.
The Dog class also defines its own bark() method.
Example
class Animal {
eat(): void {
console.log("Animal is eating");
}
}
class Dog extends Animal {
bark(): void {
console.log("Dog is barking");
}
}
const dog1 = new Dog();
dog1.eat();
dog1.bark();
Expected Output
Animal is eating
Dog is barking
Explanation
Animal
Parent class containing the eat() method.
Dog extends Animal
Child class inherits eat() and defines bark().
dog1 Object
Calls both eat() and bark().
4. Inheritance with Constructors and super()
When a child class defines its own constructor, it must call
super() before accessing this.
The super() call invokes the parent class constructor.
Example
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
displayName(): void {
console.log("Name: " + this.name);
}
}
class Student extends Person {
course: string;
constructor(name: string, course: string) {
super(name);
this.course = course;
}
displayCourse(): void {
console.log("Course: " + this.course);
}
}
const student1 = new Student("Rahul", "TypeScript");
student1.displayName();
student1.displayCourse();
Expected Output
Name: Rahul
Course: TypeScript
How super() Works
Student Object
Receives name and course values.
super(name)
Calls the Person constructor to initialize name.
Initialize Course
Student constructor assigns the course value.
super() before
using this. A child constructor must call the parent
constructor before it can finish initializing the object.
5. Method Overriding
Method overriding happens when a child class provides its own implementation of an inherited method with a compatible signature. This allows the child class to customize inherited behavior.
Example
class Animal {
makeSound(): void {
console.log("Animal makes a sound");
}
}
class Cat extends Animal {
makeSound(): void {
console.log("Cat says Meow");
}
}
const cat1 = new Cat();
cat1.makeSound();
Expected Output
Cat says Meow
Here, the Cat class overrides the inherited makeSound() method.
Calling the method on a Cat object uses the Cat implementation.
6. Multilevel Inheritance
Multilevel inheritance occurs when a class inherits from a child class, creating a chain of inheritance.
Inheritance Diagram
Animal
Base class
extends
Dog
Extends Animal
extends
Puppy
Extends Dog
Example
class Animal {
eat(): void {
console.log("Eating");
}
}
class Dog extends Animal {
bark(): void {
console.log("Barking");
}
}
class Puppy extends Dog {
weep(): void {
console.log("Puppy is weeping");
}
}
const puppy1 = new Puppy();
puppy1.eat();
puppy1.bark();
puppy1.weep();
Expected Output
Eating
Barking
Puppy is weeping
The Puppy class can access inherited public methods from both Dog and Animal.
7. Advantages of Inheritance
Code Reusability
Reuse existing properties and methods from a parent class.
Less Duplication
Keep common functionality in one shared class.
Extensibility
Add specialized behavior in child classes.
Customization
Override inherited methods to change behavior when required.
Summary
Inheritance allows a child class to reuse accessible members of a parent class
through the extends keyword.
The super() call invokes the parent constructor,
method overriding customizes inherited behavior, and multilevel inheritance
creates a chain of related classes.
Next, learn encapsulation and how to control access to an object's internal data.