Core Java - OOPs - Guided Exercise: Notifiers - Interface-Based Polymorphism #107
Replies: 4 comments
|
PRASETHA N package com.practice.problems.java;
public class Main {
public static void main(String[] args) {
Notifier email = new EmailNotifier();
Notifier sms = new SMSNotifier();
Notifier push = new PushNotifier();
NotificationService service = new NotificationService();
service.notifyUser("Welcome to the system!");
service.notifyUser(email, "Verify your email.");
service.notifyUser(sms, "Your OTP is 123456.");
service.notifyUser(push, "New message received!");
Notifier[] allNotifiers = { email, sms, push };
for (Notifier n : allNotifiers) {
n.send("System maintenance");
}
}
}
interface Notifier {
void send(String message);
}
class EmailNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("Email sent: " + message);
}
}
class SMSNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("SMS sent: " + message);
}
}
class PushNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("Push notification sent: " + message);
}
}
class NotificationService {
public void notifyUser(String message) {
System.out.println("Default notification: " + message);
}
public void notifyUser(Notifier notifier, String message) {
System.out.println("Sending via interface-based notifier...");
notifier.send(message);
}
} |
|
Lohith R class Main { } public static void logger(Notifier notifier, String message){ } public static Notifier NotifierFactory(String type){ } class PushNotifier implements Notifier { // Method Overloading: One with interface type } |
|
### Task 1 - Sayan Dey - Completed interface Notifier { class EmailNotifier implements Notifier { class SMSNotifier implements Notifier { class PushNotifier implements Notifier { } } |
|
public class Notificationservice { } interface Notifier { class EmailNotifier implements Notifier { class SMSNotifier implements Notifier { class PushNotifier implements Notifier { class NotificationService { } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
🧪 Guided Exercise: Notifiers - Interface-Based Polymorphism
🎯 Objective:
Help learners internalize how interfaces enable polymorphism, type generalization, and flexible method dispatch.
🧩 Scenario:
You're designing a notification system that can send messages via different channels: Email, SMS, and Push Notification.
Each notification type:
Implements a common
NotifierinterfaceHas its own version of
send(String message)methodCan be passed into a method that only expects a
NotifierreferenceWill be used in a polymorphic collection
This is very close to real-world design patterns and Spring Boot behavior.
📦 Step-by-Step Plan
✅ Step 1: Define the Interface
💡 Ask Learners:
Why can’t we put method bodies here?
Can we instantiate an interface?
✅ Step 2: Implement Concrete Classes
🧠 Discussion Prompt:
Which method is being overridden?
What’s common and what’s unique in each implementation?
✅ Step 3: Use Interface Reference in a Method
✍️ Ask Students to Implement:
Add logic to call this method using all three types (
EmailNotifier,SMSNotifier, etc.)Use both overloaded versions to see which one gets called
✅ Step 4: Polymorphic Collection
📘 Concepts Reinforced
🧠 Reflective Questions for Learners
Why does
n.send()in the loop behave differently for each object?What if we added a new class
WhatsAppNotifier? Do we have to change anything inNotificationService?Why is
Notifierinterface better than makingNotifieran abstract class here?🏆 Bonus Challenges
Add a method
getNotifierType()to each class that returns"Email","SMS", etc.Create a factory method to return a Notifier based on a string input (
"email","sms").Write a logger class that accepts any Notifier and logs delivery success.
All reactions