📖 Scenario: You are building a simple system to handle different types of notifications in an app. Each notification type should be able to send its message in its own way.
🎯 Goal: Create an interface called Notifier with a method send(). Then create two classes EmailNotifier and SMSNotifier that implement this interface. Finally, use polymorphism to call send() on both types of notifiers.
📋 What You'll Learn
Create an interface called Notifier with a method send() that returns void.
Create a class called EmailNotifier that implements Notifier and its send() method prints 'Sending email notification'.
Create a class called SMSNotifier that implements Notifier and its send() method prints 'Sending SMS notification'.
Create an array called notifiers of type Notifier[] containing instances of EmailNotifier and SMSNotifier.
Use a for loop to call send() on each notifier in the notifiers array.
Print the output of each send() call.
💡 Why This Matters
🌍 Real World
Many apps send notifications in different ways like email, SMS, or push notifications. Using interfaces and polymorphism helps manage these different types easily.
💼 Career
Understanding polymorphism and interfaces is key for writing flexible and maintainable code in TypeScript and many other programming languages.
Progress0 / 4 steps
1
Create the Notifier interface
Create an interface called Notifier with a method send() that returns void.
Typescript
Hint
An interface defines a contract. Here, Notifier must have a send() method that returns nothing.
2
Create EmailNotifier and SMSNotifier classes
Create a class called EmailNotifier that implements Notifier with a send() method that prints 'Sending email notification'. Also create a class called SMSNotifier that implements Notifier with a send() method that prints 'Sending SMS notification'.
Typescript
Hint
Use implements Notifier after the class name. The send() method should use console.log to print the messages.
3
Create an array of notifiers
Create an array called notifiers of type Notifier[] containing one instance of EmailNotifier and one instance of SMSNotifier.
Typescript
Hint
Create the array with the exact name notifiers and include one instance of each notifier class.
4
Call send() on each notifier and print output
Use a for loop with variable notifier to iterate over the notifiers array and call notifier.send() inside the loop.
Typescript
Hint
Use a for...of loop to call send() on each notifier. The output should show both messages.
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.