Interfaces define what methods or properties a class must have.
Classes use implements to promise they follow the interface.
Examples
Two classes implement the same interface but have different speak sounds.
Typescript
interface Speaker {
speak(): void;
}
class Dog implements Speaker {
speak() {
console.log("Woof!");
}
}
class Cat implements Speaker {
speak() {
console.log("Meow!");
}
}
Function accepts any object that implements Speaker and calls speak method.
Typescript
function makeSpeak(speaker: Speaker) {
speaker.speak();
}
const dog = new Dog();
const cat = new Cat();
makeSpeak(dog); // Woof!
makeSpeak(cat); // Meow!
Sample Program
This program shows two classes, Car and Boat, both implementing Vehicle interface. The travel function calls move on any Vehicle.
Typescript
interface Vehicle {
move(): void;
}
class Car implements Vehicle {
move() {
console.log("The car drives on the road.");
}
}
class Boat implements Vehicle {
move() {
console.log("The boat sails on the water.");
}
}
function travel(vehicle: Vehicle) {
vehicle.move();
}
const myCar = new Car();
const myBoat = new Boat();
travel(myCar);
travel(myBoat);
OutputSuccess
Important Notes
Interfaces only describe the shape; they do not create code themselves.
Polymorphism helps keep code flexible and easy to change.
Always name interfaces clearly to show what behavior they expect.
Summary
Polymorphism lets different objects be used the same way.
Interfaces define the common methods objects must have.
Classes implement interfaces to promise they have those methods.
Practice
(1/5)
1. What does polymorphism through interfaces allow in TypeScript?
easy
A. Only one class can implement an interface
B. Different classes to be treated the same if they implement the same interface
C. Interfaces can hold data like variables
D. Classes cannot implement more than one interface
Solution
Step 1: Understand polymorphism concept
Polymorphism means using different objects in the same way if they share a common interface.
Step 2: Apply to interfaces in TypeScript
Interfaces define methods that classes promise to implement, allowing different classes to be used interchangeably.
Final Answer:
Different classes to be treated the same if they implement the same interface -> Option B
Quick Check:
Polymorphism = Same interface, different classes [OK]
Hint: Polymorphism means same interface, different class objects [OK]
Common Mistakes:
Thinking interfaces hold data like variables
Believing only one class can implement an interface
Method signatures must match exactly for interface implementation. Car's move method does not match Vehicle's move method.
Final Answer:
Method move in Car has a different signature than in Vehicle interface -> Option D
Quick Check:
Method signature mismatch = Error [OK]
Hint: Method signatures must match interface exactly [OK]
Common Mistakes:
Thinking parameter difference is allowed
Believing assignment to interface type is invalid
Ignoring method return type requirements
5. You have an interface Logger with a method log(message: string): void. You want to create two classes: ConsoleLogger that logs to the console, and FileLogger that stores logs in an array. How can you use polymorphism to write a function writeLog(logger: Logger, msg: string) that works with both classes? Which implementation is correct?
hard
A. function writeLog(logger: Logger, msg: string) { logger.log(msg); }
B. function writeLog(logger: Logger, msg: string) { console.log(msg); }
C. function writeLog(logger: Logger, msg: string) { logger.write(msg); }
D. function writeLog(logger: Logger, msg: string) { logger.log(); }
Solution
Step 1: Understand polymorphism with interfaces
The function should accept any object implementing Logger and call its log method with the message.
Step 2: Check each function implementation
function writeLog(logger: Logger, msg: string) { logger.log(msg); } calls logger.log(msg) correctly. function writeLog(logger: Logger, msg: string) { console.log(msg); } ignores the passed logger and logs directly to console. function writeLog(logger: Logger, msg: string) { logger.write(msg); } calls a non-existent method write. function writeLog(logger: Logger, msg: string) { logger.log(); } calls log without arguments, which is incorrect.
Final Answer:
function writeLog(logger: Logger, msg: string) { logger.log(msg); } -> Option A
Quick Check:
Call interface method with correct argument [OK]
Hint: Call the interface method with the message parameter [OK]
Common Mistakes:
Calling wrong method name
Not passing the message argument
Ignoring the logger parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.