Polymorphism means one interface can represent different types of objects. It lets you use the same method or property name for different behaviors.
Click to reveal answer
beginner
How do interfaces help achieve polymorphism in TypeScript?
Interfaces define a contract with methods or properties. Different classes can implement the same interface but behave differently, enabling polymorphism.
Click to reveal answer
beginner
Example: What does this interface represent?
interface Animal { speak(): void; }
It represents any animal that can speak. Different animals can implement speak() in their own way.
Click to reveal answer
intermediate
What happens when you call a method on an interface type variable?
The actual method of the object assigned to that variable runs. This is polymorphism: same call, different results.
Click to reveal answer
beginner
Why is polymorphism useful in programming?
It lets you write flexible and reusable code. You can work with different objects through the same interface without knowing their exact types.
Click to reveal answer
What does polymorphism allow you to do with interfaces in TypeScript?
ACreate only one class from an interface
BUse the same interface for different classes with different behaviors
CPrevent classes from sharing methods
DForce all classes to have the same method implementation
✗ Incorrect
Polymorphism lets different classes implement the same interface but behave differently.
Which keyword is used to make a class follow an interface in TypeScript?
Ainherits
Bextends
Cimplements
Duses
✗ Incorrect
The 'implements' keyword tells TypeScript that a class follows an interface.
If two classes implement the same interface, what can you do with their objects?
AUse them interchangeably where the interface type is expected
BOnly use one class at a time
CConvert one class into the other automatically
DThey cannot be used together
✗ Incorrect
Objects of classes implementing the same interface can be used interchangeably where that interface is expected.
What will happen if a class does not implement all methods of an interface?
ATypeScript will show an error
BThe class will work fine
CThe interface will change automatically
DThe missing methods will be ignored
✗ Incorrect
TypeScript requires all interface methods to be implemented, otherwise it shows an error.
Which of these is an example of polymorphism through interfaces?
AWriting functions without parameters
BOne class inherits from another without interfaces
CUsing variables without types
DDifferent classes implement the same interface method differently
✗ Incorrect
Polymorphism means different classes implement the same interface method in their own way.
Explain how polymorphism works with interfaces in TypeScript.
Think about how one interface can represent many types.
You got /4 concepts.
Describe a real-life example that shows polymorphism through interfaces.
Imagine animals making sounds differently but all can speak.
You got /3 concepts.
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.