-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicMethodDispatch.java
More file actions
62 lines (52 loc) · 1.51 KB
/
DynamicMethodDispatch.java
File metadata and controls
62 lines (52 loc) · 1.51 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
54
55
56
57
58
59
60
61
62
class Phone {
public void on() {
System.out.println("Starting the phone....");
}
public void greetings() {
System.out.println("Hello World....!!");
}
}
class SmartPhone extends Phone {
public void on() {
System.out.println("Starting the Smart Phone...");
}
public void music() {
System.out.println("Playing Music....");
}
}
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}
public class DynamicMethodDispatch {
public static void main(String[] args) {
// dynamic dispatch
// runtime polymorphism
Phone obj = new SmartPhone();
obj.on();//Starting the Smart Phone...
// obj.music();//It will show an error
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}