-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP46_MethodOverridingPolymorphism.java
More file actions
53 lines (45 loc) · 1.56 KB
/
Copy pathP46_MethodOverridingPolymorphism.java
File metadata and controls
53 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package programs;
/**
* ============================================================
* PROGRAM 46: Method Overriding and Runtime Polymorphism
* ============================================================
* Problem: WAP to demonstrate dynamic method dispatch where
* a superclass reference invokes overridden methods of different subclasses.
* ============================================================
*/
class Notification {
public void send(String message) {
System.out.println(" [Base] Generic notification: " + message);
}
}
class EmailNotification extends Notification {
@Override
public void send(String message) {
System.out.println(" 📧 [Email] Sending email with subject/body: " + message);
}
}
class SmsNotification extends Notification {
@Override
public void send(String message) {
System.out.println(" 📱 [SMS] Sending 160-char SMS: " + message);
}
}
class PushNotification extends Notification {
@Override
public void send(String message) {
System.out.println(" 🔔 [Push] Sending mobile push notification: " + message);
}
}
public class P46_MethodOverridingPolymorphism {
public static void main(String[] args) {
Notification[] channels = {
new EmailNotification(),
new SmsNotification(),
new PushNotification()
};
System.out.println("=== RUNTIME POLYMORPHISM (DYNAMIC DISPATCH) ===");
for (Notification channel : channels) {
channel.send("Your security code is 987-123");
}
}
}