📖 Scenario: You are building a simple system to manage different types of vehicles. Each vehicle must have a method to display its details.
🎯 Goal: Create an interface called Vehicle with a method displayInfo(). Then create a class Car that implements this interface and shows the car's brand and model.
📋 What You'll Learn
Create an interface named Vehicle with a method displayInfo() that returns void
Create a class named Car that implements the Vehicle interface
Add a constructor to Car that takes brand and model as string parameters
Implement the displayInfo() method in Car to print the brand and model
Create an instance of Car with brand "Toyota" and model "Corolla"
Call the displayInfo() method on the Car instance
💡 Why This Matters
🌍 Real World
Interfaces help define clear contracts for classes in large applications, ensuring consistent behavior across different objects.
💼 Career
Understanding interfaces and class implementation is essential for TypeScript developers building scalable and maintainable codebases.
Progress0 / 4 steps
1
Create the Vehicle interface
Create an interface called Vehicle with a method displayInfo() that returns void.
Typescript
Hint
Use the interface keyword and define displayInfo() with no parameters and return type void.
2
Create the Car class implementing Vehicle
Create a class called Car that implements the Vehicle interface. Add a constructor with parameters brand and model of type string, and store them as public properties.
Typescript
Hint
Use implements Vehicle after the class name. Use public in constructor parameters to create properties automatically.
3
Implement the displayInfo() method
Inside the Car class, implement the displayInfo() method to print the message "Car: {brand} {model}" using console.log and template strings.
Typescript
Hint
Use console.log with backticks ` and ${} to insert variables.
4
Create Car instance and call displayInfo()
Create a variable myCar as a new Car with brand "Toyota" and model "Corolla". Then call myCar.displayInfo() to print the car details.
Typescript
Hint
Create the instance with new Car("Toyota", "Corolla") and call displayInfo().
Practice
(1/5)
1. What does the implements keyword do in a TypeScript class?
easy
A. It ensures the class follows the structure defined by an interface.
B. It creates a new interface inside the class.
C. It imports an interface from another file.
D. It automatically generates methods for the class.
Solution
Step 1: Understand the role of interfaces
Interfaces define a contract of properties and methods that a class must have.
Step 2: Role of implements keyword
The implements keyword makes sure the class follows that contract exactly.
Final Answer:
It ensures the class follows the structure defined by an interface. -> Option A
Quick Check:
implements enforces interface structure [OK]
Hint: Remember: implements means 'follow the interface rules' [OK]
Common Mistakes:
Thinking implements creates or imports interfaces
Believing implements auto-generates code
Confusing implements with extends
2. Which of the following is the correct syntax to make a class Car implement an interface Vehicle?
easy
A. interface Car implements Vehicle {}
B. class Car implements Vehicle {}
C. class Car extends Vehicle {}
D. class Car uses Vehicle {}
Solution
Step 1: Identify correct keywords for class and interface
Classes use the keyword implements to follow interfaces.
Step 2: Check syntax correctness
class Car implements Vehicle {} is the correct way to declare this.
Final Answer:
class Car implements Vehicle {} -> Option B
Quick Check:
Class uses implements for interfaces [OK]
Hint: Use 'implements' keyword after class name for interfaces [OK]
Common Mistakes:
Using extends instead of implements for interfaces
Trying to implement interface in interface declaration
Using 'uses' keyword which doesn't exist
3. What will be the output of this code?
interface Animal {
name: string;
speak(): string;
}
class Dog implements Animal {
name: string;
constructor(name: string) {
this.name = name;
}
speak() {
return `${this.name} says woof`;
}
}
const dog = new Dog('Buddy');
console.log(dog.speak());
medium
A. undefined
B. "Dog says woof"
C. "Buddy says woof"
D. Error: Dog does not implement speak()
Solution
Step 1: Check class implements interface correctly
Dog class has property name and method speak() as required.
Step 2: Understand method output
speak() returns a string with the dog's name and 'says woof'.
Final Answer:
"Buddy says woof" -> Option C
Quick Check:
Method returns correct string with name [OK]
Hint: Look for method return values matching console.log output [OK]