-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDynamicMethodDispatch1.java
58 lines (45 loc) · 1.05 KB
/
DynamicMethodDispatch1.java
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
class A {
A() {
System.out.println("A's Constructor");
}
void method() {
System.out.println("A's Method");
}
}
class B extends A {
B() {
System.out.println("B's Constructor");
}
void method() {
System.out.println("B's Method");
}
}
class C extends A {
C() {
System.out.println("C's Constructor");
}
void method() {
System.out.println("C's Method");
}
}
class DynamicMethodDispatch1 {
public static void main(String[] args) {
A a=new A();
B b=new B();
C c=new C();
// obtain a reference of type A
A ref;
// ref refers to an A object
ref = a;
// calling A's version of m1()
ref.method();
// now ref refers to a B object
ref = b;
// calling B's version of m1()
ref.method();
// now ref refers to a C object
ref = c;
// calling C's version of m1()
ref.method();
}
}