Understanding Inheritance in TypeScript
Introduction to Inheritance
Welcome back to our exploration of Object-Oriented Programming (OOP) in TypeScript. This unit's topic is Inheritance, a feature that can significantly enhance code reusability and efficiency.
Inheritance: First Example
Programming, especially OOP, often mirrors concepts found in the real world. Inheritance is one such trait - Child classes inherit characteristics from their Parent classes, akin to genetics in biology.
Here is a concrete TypeScript demonstration of a Child class inheriting from a Parent class:
In this instance, the Child class inherits from the Parent class, thereby sharing the greet() method. This is inheritance in action!
The 'extends' Keyword
We introduce the keyword extends for implementing inheritance. It's a potent keyword that allows a new class to inherit properties and methods from an existing class. In short, a Child class extends from a Parent class.
For example, a Car extends a Vehicle. While both have the potential for movement, a Car has additional specific attributes — such as the number of wheels.
In this demonstration, the Car class extends the Vehicle class, adopting the move() method.
