Skip to content

Commit 0ee2506

Browse files
Dynamic Method Dispatch
1 parent afa5314 commit 0ee2506

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Phone {
2+
public void showTime() {
3+
System.out.println("Time is 8 am");
4+
}
5+
public void on() {
6+
System.out.println("Turning on Phone...");
7+
}
8+
}
9+
10+
class SmartPhone extends Phone {
11+
public void music() {
12+
System.out.println("Playing music...");
13+
}
14+
public void on() {
15+
System.out.println("Turning on SmartPhone...");
16+
}
17+
}
18+
public class cwh_49_dynamic_method_dispatch {
19+
public static void main(String[] args) {
20+
// Phone obj = new Phone(); // Allowed
21+
// SmartPhone smobj = new SmartPhone(); // Allowed
22+
// obj.name();
23+
24+
Phone obj = new SmartPhone(); // Yes it is allowed
25+
// SmartPhone obj2 = new Phone(); // Not allowed
26+
27+
obj.showTime();
28+
obj.on();
29+
// obj.music(); Not Allowed
30+
}
31+
}

0 commit comments

Comments
 (0)