Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and methods from another class. This tutorial will guide you through implementing inheritance using classes in TypeScript, providing both theoretical understanding and practical examples.
Inheritance enables the creation of a new class based on an existing class. The existing class is called the base class or parent class, while the newly created class is called the derived class or child class. The derived class inherits all the properties and methods of the base class, which promotes code reusability and establishes a hierarchical relationship between classes.
Let's dive into some practical examples to understand how inheritance works in TypeScript.
Consider a simple scenario where we have two classes, Animal and Dog. The Dog class inherits from the Animal class.
1class Animal {2name: string;34constructor(name: string) {5this.name = name;6}78makeSound(): void {9console.log("Some generic animal sound");10}11}1213class Dog extends Animal {14breed: string;1516constructor(name: string, breed: string) {17super(name);18this.breed = breed;19}2021bark(): void {22console.log(`${this.name} barks loudly!`);23}24}2526const myDog = new Dog("Buddy", "Golden Retriever");27myDog.makeSound(); // Output: Some generic animal sound28myDog.bark(); // Output: Buddy barks loudly!
In this example:
Animal class has a property name and a method makeSound().Dog class extends the Animal class using the extends keyword.Dog class has an additional property breed and a method bark().Dog class calls the constructor of the Animal class using super(name) to initialize the inherited properties.Inheritance allows derived classes to override methods from the base class. Let's modify the previous example to demonstrate method overriding.
1class Animal {2name: string;34constructor(name: string) {5this.name = name;6}78makeSound(): void {9console.log("Some generic animal sound");10}11}1213class Dog extends Animal {14breed: string;1516constructor(name: string, breed: string) {17super(name);18this.breed = breed;19}2021makeSound(): void {22console.log(`${this.name} barks loudly!`);23}2425bark(): void {26console.log(`${this.name} barks loudly!`);27}28}2930const myDog = new Dog("Buddy", "Golden Retriever");31myDog.makeSound(); // Output: Buddy barks loudly!
In this modified example:
makeSound() method in the Dog class overrides the makeSound() method in the Animal class.makeSound() is called on an instance of Dog, it executes the overridden method, which prints a specific message for dogs.TypeScript supports access modifiers like public, protected, and private. Let's explore how these modifiers affect inheritance.
1class Animal {2protected name: string;34constructor(name: string) {5this.name = name;6}78makeSound(): void {9console.log("Some generic animal sound");10}11}1213class Dog extends Animal {14private breed: string;1516constructor(name: string, breed: string) {17super(name);18this.breed = breed;19}2021displayInfo(): void {22console.log(`Name: ${this.name}, Breed: ${this.breed}`);23}24}2526const myDog = new Dog("Buddy", "Golden Retriever");27myDog.displayInfo(); // Output: Name: Buddy, Breed: Golden Retriever28// myDog.name; // Error: Property 'name' is protected and only accessible within class 'Animal' and its subclasses.
In this example:
name property in the Animal class is marked as protected, allowing it to be accessed by derived classes but not from outside the class hierarchy.breed property in the Dog class is marked as private, making it accessible only within the Dog class itself.In this tutorial, we explored the basics of inheritance in TypeScript using classes. In the next section, we will delve into Abstract Classes, which are a more advanced concept that allows you to define a base class with some methods that must be implemented by derived classes. This will provide a deeper understanding of how to structure and organize your code effectively.
Stay tuned for more insights into object-oriented programming in TypeScript!