-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileTimeResolution.java
More file actions
43 lines (31 loc) · 947 Bytes
/
CompileTimeResolution.java
File metadata and controls
43 lines (31 loc) · 947 Bytes
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
/**
* This demonstrates how Java polymorphism is limited to the compile time type.
*/
public class CompileTimeResolution {
public static void main(String[] args) {
SomethingA a = new SomethingA();
SomethingA b = new SomethingB();
SomethingA c = new SomethingC();
sayHello(a);
sayHello(b);
// Explicitly casting to the runtime type of the object makes no difference
sayHello(c.getClass().cast(c));
}
private static void sayHello(SomethingA a) {
System.out.println("Hello A");
}
// Never called
private static void sayHello(SomethingB b) {
System.out.println("Hello B");
}
// Never called
private static void sayHello(SomethingC c) {
System.out.println("Hello C");
}
static class SomethingA {
}
static class SomethingB extends SomethingA {
}
static class SomethingC extends SomethingB {
}
}