File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
49.Dynamic_Method_Dispatch Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments