The INHERITANCE is a mechanism that allows us to define a new class based on an existing class. The existing class is known as the base class or superclass, and the new class is known as the derived class or subclass.
The subclass inherits all the properties and methods of the superclass and can add new attributes and behaviors as needed.
If you want to learn more about Inheritance and Polymorphism
check out the Object-Oriented Programming Course read more ⯈
In TypeScript, we can achieve inheritance using the keyword extends
. The base class defines the common properties and methods, while the derived class extends and specializes these members.
Let’s see it with an example:
// Base class
class Animal {
constructor(public name: string) {}
makeSound(): void {
console.log(`${this.name} makes a sound.`);
}
}
// Derived class
class Dog extends Animal {
bark(): void {
console.log(`${this.name} is barking.`);
}
}
const myDog = new Dog("MyDog");
myDog.makeSound(); // Output: MyDog makes a sound.
myDog.bark(); // Output: MyDog is barking.
In this example,
Dog
inherits the propertyname
and the methodmakeSound
from the classAnimal
- Adds its own method
bark
Method Overriding
A derived class can override methods of the base class using the keyword override
.
class Animal {
constructor(public name: string) {}
makeSound(): void {
console.log(`${this.name} makes a sound.`);
}
}
class Cat extends Animal {
override makeSound(): void {
console.log(`${this.name} meows.`);
}
}
const myCat = new Cat("Misi");
myCat.makeSound(); // Output: Misi meows.
Here, the method makeSound
in Cat
overrides the implementation of the method makeSound
in Animal
.
Constructor and Initialization in Inheritance
When a derived class has a constructor, it is generally necessary to call the constructor of the base class to properly initialize the object. To do this, we use the keyword super
.
class Animal {
constructor(public name: string) {}
}
class Dog extends Animal {
constructor(name: string, public breed: string) {
super(name); // Calls the constructor of the base class
}
showInfo(): void {
console.log(`Name: ${this.name}, Breed: ${this.breed}`);
}
}
const myDog = new Dog("Rex", "Labrador");
myDog.showInfo(); // Output: Name: Rex, Breed: Labrador
In this example, the constructor of Dog
calls the constructor of Animal
using super
to initialize the property name
.
Polymorphism in TypeScript
The POLYMORPHISM is another key concept in object-oriented programming. It allows us to treat objects of different classes uniformly, as long as they share a common interface or superclass.
In TypeScript, we can achieve polymorphism using interfaces. Let’s see an example using an interface:
interface Animal {
makeSound(): void;
}
class Dog implements Animal {
makeSound() {
console.log("The dog barks");
}
}
class Cat implements Animal {
makeSound() {
console.log("The cat meows");
}
}
function makeAnimalSound(animal: Animal) {
animal.makeSound();
}
const myDog = new Dog();
const myCat = new Cat();
makeAnimalSound(myDog); // Output: "The dog barks"
makeAnimalSound(myCat); // Output: "The cat meows"
In this example
- We create an interface
Animal
with a methodmakeSound
. - Then, we create the classes
Dog
andCat
that implement the interfaceAnimal
. - Finally, we create a function
makeAnimalSound
that accepts a parameter of typeAnimal
and calls themakeSound
method of the passed object.