Object-oriented programming (OOP) is a widely used programming paradigm that focuses on creating reusable and modular code. One of the key concepts in OOP is inheritance, which allows classes to inherit properties and methods from other classes. Another important concept is polymorphism, which allows objects of different classes to be treated as objects of a common parent class. In this article, we will explore how inheritance and polymorphism are implemented in TypeScript.
Inheritance is a mechanism that enables a class to inherit properties and methods from another class, known as the parent class or super class. The class that inherits from the parent class is called the child class or sub class. In TypeScript, we can achieve inheritance using the extends
keyword.
Consider the following example:
class Animal {
constructor(public name: string) {}
eat() {
console.log(this.name + ' is eating.');
}
}
class Dog extends Animal {
bark() {
console.log('Woof!');
}
}
const dog = new Dog('Max');
dog.eat(); // Output: Max is eating.
dog.bark(); // Output: Woof!
In this example, the Animal
class serves as the parent class, and the Dog
class inherits from it using the extends
keyword. The Dog
class not only inherits the name
property and eat()
method from the Animal
class but also adds its own unique bark()
method.
Polymorphism is the ability of objects of different classes to be treated as objects of a common parent class. This allows for greater flexibility and code reusability. TypeScript supports polymorphism through method overriding.
Consider the following example:
class Animal {
constructor(public name: string) {}
makeSound() {
console.log('Unknown sound');
}
}
class Dog extends Animal {
makeSound() {
console.log('Woof!');
}
}
class Cat extends Animal {
makeSound() {
console.log('Meow!');
}
}
function makeAnimalSound(animal: Animal) {
animal.makeSound();
}
const dog = new Dog('Max');
const cat = new Cat('Whiskers');
makeAnimalSound(dog); // Output: Woof!
makeAnimalSound(cat); // Output: Meow!
In this example, the Animal
class has a makeSound()
method that prints 'Unknown sound'. The Dog
and Cat
classes override this method with their own specific implementation. The makeAnimalSound()
function accepts an object of type Animal
, which means it can accept objects of either the Dog
or Cat
class. When we call this function with a Dog
object, it prints 'Woof!', and when we call it with a Cat
object, it prints 'Meow!'.
This demonstrates how different objects, even though they belong to different classes, can be treated as objects of a common parent class, allowing for code modularity and flexibility.
Inheritance and polymorphism are essential concepts in object-oriented programming that contribute to code reusability and flexibility. In TypeScript, these concepts can be effectively implemented using the extends
keyword for inheritance and method overriding for polymorphism. Understanding and properly utilizing inheritance and polymorphism can greatly enhance the structure and functionality of your TypeScript applications.
noob to master © copyleft