In Typescript, Inheritance allows a class (subclass or derived class) to inherit properties and methods from another class (superclass or base class).
Typescript supports single inheritance, meaning a class can inherit from only one other class.
// Base class (superclass) class Vehicle { private _carType: string; constructor(_carType: string) { this._carType = _carType; } // Getter for name get carType(): string { return this._carType; } // Method makeSound(): void { console.log("pee pee!"); } }
Here, we have declared the private `_carType` attribute, the getter method to access the `carType` attribute value and the additional method `makeSound`.
we didn't declare a setter method for attribute `_carType` because we will assign value to the `_carType` variable while creating an instance of a class and it will automatically invoke the constructor method.
// Derived class (subclass) class Car extends Vehicle { private name: string; constructor(name: string, _carType: string) { super(_carType); // Call the constructor of the base class this.name = name; } // Method override makeSound(): void { console.log("pee pee pee pee!"); } // Additional method getCarName(): string { return this.name; } }
Here, we have declared the private `name` attribute, the `makeSound` method is overridden in the subclass and an additional method to `getCarName`.
// Base class (superclass) class Vehicle { private _carType: string; constructor(_carType: string) { this._carType = _carType; } // Getter for name get carType(): string { return this._carType; } // Method makeSound(): void { console.log("pee pee!"); } } // Derived class (subclass) class Car extends Vehicle { private name: string; constructor(name: string, _carType: string) { super(_carType); // Call the constructor of the base class this.name = name; } // Method override makeSound(): void { console.log("pee pee pee pee!"); } // Additional method getCarName(): string { return this.name; } } // Creating instances let vehicle: Vehicle = new Vehicle("SUV"); let car: Car = new Car("Defender", "SUV"); // Accessing base class method vehicle.makeSound(); // Output: pee pee! // Accessing overridden method in the derived class car.makeSound(); // Output: pee pee pee pee! // Accessing additional method in the derived class console.log(car.getCarName()); // Output: Defender
The "Vehicle" class is the base class with a private property `_carType`, a getter for the `carType`, and a method `makeSound`.
The Car class is the derived class that extends `Vehicle`. It has an additional private property name, a "constructor" that calls the base class "constructor" using "super", an overridden "makeSound" method, and an additional method "getCarName".
Instances of both the base and derived classes are created ("vehicle" and "car").
The derived class overrides the "makeSound" method to provide a specific implementation for "cars".
The derived class has an additional method, `getCarName`, which is not present in the base class.
Through inheritance, The "Car" class inherits properties and methods from the `Vehicle` class, and it can provide its own implementation or add additional functionality. This helps in code reuse and structuring code in a more organized way.