Vehicle with a method describe() that returns a string.Car that extends Vehicle.describe() method in Car to return a more specific type: an object with make and model properties.describe() on both Vehicle and Car instances.Vehicle with a method describe() that returns a string.Car that extends Vehicle.describe() method in Car to return a more specific type: an object with make and model properties.describe() on both Vehicle and Car instances.Vehicle with a method describe() that returns the string 'This is a vehicle.'Use class Vehicle {} and inside it define describe(): string { return 'This is a vehicle.'; }
Car that extends Vehicle.Use class Car extends Vehicle {} to create the subclass.
Car class, override the describe() method to return an object with make and model properties, both strings. For example, return { make: 'Toyota', model: 'Corolla' }.Define describe() with return type { make: string; model: string } and return the object.
Vehicle called vehicle and an instance of Car called car. Then print the results of vehicle.describe() and car.describe() using console.log.Use const vehicle = new Vehicle(); and const car = new Car();, then console.log(vehicle.describe()); and console.log(car.describe());.
What does method overriding mean in TypeScript?
Which of the following is the correct syntax for overriding a method greet() in a child class in TypeScript?
class Parent {
greet(): string {
return "Hello";
}
}
class Child extends Parent {
?
}What will be the output of this TypeScript code?
class Animal {
speak(): string {
return "Animal sound";
}
}
class Dog extends Animal {
speak(): string {
return "Bark";
}
}
const pet: Animal = new Dog();
console.log(pet.speak());Identify the error in this TypeScript code that tries to override a method:
class Vehicle {
move(distance: number): void {
console.log(`Moved ${distance} meters`);
}
}
class Car extends Vehicle {
move(distance: string): void {
console.log(`Car moved ${distance}`);
}
}Given these classes, which override is valid and why?
class Base {
calculate(value: number): number {
return value * 2;
}
}
class Derived extends Base {
calculate(value: number): number | string {
if (value > 10) return "Too large";
return value * 3;
}
}Pass the test and this pattern moves unknown