TypeScript Class Inheritance
TypeScript Classes and OOP : Exercise-3 with Solution
Write a TypeScript class called SUV (Sports Utility Vehicle) that extends the Car class. Add a property to represent whether the SUV is suitable for off-road driving. Implement a method that toggles off-road capability and prints a message accordingly.
Sample Solution:
TypeScript Code:
// Define a base class 'Car'
class Car {
// Properties
make: string;
model: string;
year: number;
// Constructor
constructor(make: string, model: string, year: number) {
this.make = make;
this.model = model;
this.year = year;
}
// Method to start the car
start() {
console.log(`The ${this.make} ${this.model} (Year: ${this.year}) is starting.`);
}
}
// Define a derived class 'SUV' that extends 'Car'
class SUV extends Car {
// Property to represent off-road capability
offRoadCapable: boolean;
// Constructor for SUV
constructor(make: string, model: string, year: number, offRoadCapable: boolean) {
// Call the base class constructor
super(make, model, year);
// Initialize the off-road capability property
this.offRoadCapable = offRoadCapable;
}
// Method to toggle off-road capability and print a message
toggleOffRoadMode() {
if (this.offRoadCapable) {
console.log(`The ${this.make} ${this.model} is now in off-road mode.`);
} else {
console.log(`The ${this.make} ${this.model} is not suitable for off-road driving.`);
}
}
}
// Create an SUV object
const mySUV = new SUV("Toyota", "Fortuner", 2023, true);
// Call the start method to start the SUV
mySUV.start();
// Toggle the off-road mode
mySUV.toggleOffRoadMode();
Explanations:
In the exercise above -
- First, we define a base class "Car" with properties 'make', 'model', and 'year', along with a "start()" method that prints a starting message.
- Define a derived class "SUV" that extends 'Car'. It includes an additional property offRoadCapable to represent whether the SUV is suitable for off-road driving.
- The constructor for the "SUV" class takes parameters for 'make', 'model', 'year', and 'offRoadCapable'. It calls the base class constructor using super() to initialize the inherited properties.
- Implement a "toggleOffRoadMode()" method in the "SUV" class that toggles the off-road capability and prints a message accordingly.
- Finally, we create an instance of the "SUV" class called 'mySUV', call the "start()" method to start the SUV, and then toggle the off-road mode to demonstrate the off-road capability feature.
Output:
"The Toyota Fortuner (Year: 2023) is starting." "The Toyota Fortuner is now in off-road mode."
TypeScript Editor:
See the Pen TypeScript by w3resource (@w3resource) on CodePen.
Previous: TypeScript Class with Methods.
Next: TypeScript Class Composition.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/typescript-exercises/typescript-class-and-oop-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics