📖 Scenario: You are creating a simple program to represent different types of vehicles. Each vehicle has a method to describe its sound, but the exact sound depends on the type of vehicle.
🎯 Goal: Build a TypeScript program using an abstract class called Vehicle with an abstract method makeSound(). Then create two classes Car and Motorcycle that extend Vehicle and implement the makeSound() method. Finally, create instances and print their sounds.
📋 What You'll Learn
Create an abstract class called Vehicle with an abstract method makeSound() that returns a string.
Create a class Car that extends Vehicle and implements makeSound() to return the string 'Vroom'.
Create a class Motorcycle that extends Vehicle and implements makeSound() to return the string 'Braaap'.
Create one instance of Car called myCar and one instance of Motorcycle called myMotorcycle.
Print the result of calling makeSound() on myCar and myMotorcycle.
💡 Why This Matters
🌍 Real World
Abstract classes help organize code when you have a general concept with shared behavior but different specific details, like different types of vehicles.
💼 Career
Understanding abstract classes is important for designing clean, reusable code in many software development jobs, especially when working with object-oriented programming.
Progress0 / 4 steps
1
Create the abstract class Vehicle
Create an abstract class called Vehicle with an abstract method makeSound() that returns a string.
Typescript
Hint
Use the abstract keyword before the class and the method. The method should have no body.
2
Create Car and Motorcycle classes
Create a class Car that extends Vehicle and implements makeSound() to return 'Vroom'. Also create a class Motorcycle that extends Vehicle and implements makeSound() to return 'Braaap'.
Typescript
Hint
Use extends Vehicle to inherit. Implement makeSound() with the correct return strings.
3
Create instances of Car and Motorcycle
Create one instance of Car called myCar and one instance of Motorcycle called myMotorcycle.
Typescript
Hint
Use new Car() and new Motorcycle() to create instances.
4
Print the sounds of the vehicles
Print the result of calling makeSound() on myCar and myMotorcycle.
Typescript
Hint
Use console.log() to print the sounds.
Practice
(1/5)
1. What is true about an abstract class in TypeScript?
easy
A. Abstract classes are the same as interfaces.
B. Abstract classes cannot have any methods implemented.
C. You cannot create an instance of an abstract class directly.
D. You must use the new keyword to instantiate an abstract class.
Solution
Step 1: Understand abstract class instantiation rules
Abstract classes cannot be instantiated directly, so you cannot create objects from them using new.
Step 2: Compare other options
Abstract classes can have implemented methods, are different from interfaces, and cannot be instantiated with new.
Final Answer:
You cannot create an instance of an abstract class directly. -> Option C
Quick Check:
Abstract class instantiation = not allowed [OK]
Hint: Remember: abstract classes can't be directly instantiated [OK]
Common Mistakes:
Thinking abstract classes are interfaces
Assuming abstract classes can't have implemented methods
Trying to instantiate abstract classes directly
2. Which of the following is the correct syntax to declare an abstract class in TypeScript?
easy
A. abstract class Vehicle {}
B. class abstract Vehicle {}
C. abstract Vehicle class {}
D. class Vehicle abstract {}
Solution
Step 1: Recall TypeScript abstract class syntax
The correct syntax places the keyword abstract before class, followed by the class name.
Step 2: Check each option
Only abstract class Vehicle {} correctly uses abstract class Vehicle {}. Others have incorrect keyword order.
Final Answer:
abstract class Vehicle {} -> Option A
Quick Check:
Abstract class syntax = abstract class Name {} [OK]
Hint: Put 'abstract' before 'class' keyword [OK]
Common Mistakes:
Placing 'abstract' after 'class'
Mixing keyword order
Omitting 'abstract' keyword
3. What will be the output of the following TypeScript code?
abstract class Animal {
abstract makeSound(): void;
move(): void {
console.log('Moving');
}
}
class Dog extends Animal {
makeSound(): void {
console.log('Bark');
}
}
const dog = new Dog();
dog.makeSound();
dog.move();
medium
A. Moving\nBark
B. No output
C. Error: Cannot create instance of abstract class
D. Bark\nMoving
Solution
Step 1: Understand class inheritance and method calls
The class Dog extends Animal and implements makeSound(). Calling dog.makeSound() prints 'Bark'.
Step 2: Check the move() method call
The move() method is implemented in Animal and prints 'Moving'. Calling dog.move() prints 'Moving'.
Final Answer:
Bark
Moving -> Option D
Quick Check:
Subclass methods run, abstract class methods inherited [OK]
Hint: Abstract methods must be implemented; inherited methods run normally [OK]
Common Mistakes:
Thinking abstract class instance is created
Confusing method call order
Expecting errors on calling inherited methods
4. Identify the error in the following TypeScript code:
abstract class Shape {
abstract area(): number;
}
class Circle extends Shape {
radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
}
medium
A. Circle must implement the abstract method area()
B. Abstract class cannot have abstract methods
C. radius property should be private
D. Missing call to super() in Circle constructor
Solution
Step 1: Check abstract method implementation
The abstract class Shape declares an abstract method area(). Any subclass must implement this method.
Step 2: Verify Circle class methods
The Circle class does not implement area(), so this causes a compile-time error.
Final Answer:
Circle must implement the abstract method area() -> Option A