Skip to content

Commit b13b89e

Browse files
committed
complete example on method override and overload
1 parent c012797 commit b13b89e

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

src/section9/MethodOverloading.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static double m(int x, double y) {
1919
return x + y + 1;
2020
}
2121

22-
/* Cannot have the following method, otherwise calling m(12, 12) would be illegal!
22+
/* Cannot have the following method, otherwise calling m(12, 12) would be illegal (since it's ambiguous)!
2323
static double m(double x, int y) {
2424
return x + y + 1;
2525
}

src/section9/MethodOverriding.java

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package section9;
2+
3+
/**
4+
* This class demonstrates the effect of method overloading, overriding, and hiding across classes.
5+
* Created by jzong on 3/8/18.
6+
*/
7+
public class MethodOverriding {
8+
public static void main(String[] argv) {
9+
int i = 42;
10+
Integer ii = new Integer(i);
11+
double d = 3.14;
12+
13+
/* Instantiate C1 and C2, and then assign C2 to C1 type variable. */
14+
C1 nativeC1 = new C1();
15+
C2 c2 = new C2();
16+
C1 c2AsC1 = c2;
17+
18+
/**
19+
* Overall principle:
20+
* If signature matching based on variable type (*not* underlying object type) returns static method, then
21+
* simply use VariableType.staticMethod().
22+
* Otherwise, check for a fitting method starting from the *underlying* object type (in this case C2 then C1).
23+
*
24+
* In order to check for matching signature, follow the three tiers:
25+
* - no [un-]boxing or param lists allowed
26+
* - only [un-]boxing allowed
27+
* - both allowed
28+
*/
29+
// Trivial section with only C1
30+
nativeC1.m1(d); // Calls static C1.m1 due to matching signature
31+
nativeC1.m1(i); // Calls non-static C1.m1 due to more specific signature
32+
nativeC1.m1(ii); // Calls non-static C1.m1 after unboxing
33+
nativeC1.m2(i); // Trivial due to exact signature match
34+
35+
/**
36+
* Here, Target Type is always C1. We find a matching signature in C1 first, and depending on whether it's
37+
* a static method, we pick the static method from C1, or pick the non-static method by starting looking
38+
* at C2, and then C1 (if not found in C2).
39+
*
40+
* Signature lookup follows the three tiers above.
41+
*/
42+
43+
System.out.println();
44+
c2AsC1.m1(d); // Target Type: C1; Target Sig: static m1(d); Result: C1.m1(d)
45+
c2AsC1.m1(i); // Target Type: C1; Target Sig: m1(int); Result: C2.m1(int)
46+
c2AsC1.m1(ii); // Target Type: C1; Target Sig: m1(int) after unboxing; Result: C2.m1(int)
47+
c2AsC1.m2(i); // Target Type: C1; Target Sig: m2(int); Result: C1.m2(int) since no unboxing at first
48+
49+
/**
50+
* Here, Target Type is always C2. We find the matching signature in C2 (and C1 if doesn't exist).
51+
* Signature lookup follows the three tiers above.
52+
*/
53+
System.out.println();
54+
c2.m1(i); // Target Type: C2; Target Sig: m1(int); Result: C2.m1(int)
55+
c2.m1(d); // Target Type: C2; Target Sig: static m1(double); Result: C2.m1(double)
56+
c2.m2(i); // Target Type: C2; Target Sig: m2(int) w/o unboxing; Result: C1.m2(int)
57+
c2.m2(ii); // Target Type: C2; Target Sig: m2(Integer) match; Result: C2.m2(Integer)
58+
c2.m3(ii); // Target Type: C2; Target Sig: m3(int) w/ unboxing; Result: C2.m3(int)
59+
c2.m4(i); // Target Type: C2; Target Sig: m4(Integer) w/ boxing; Result: C2.m4(Integer)
60+
}
61+
}
62+
63+
class C1 {
64+
static void m1(double d) {
65+
System.out.println("Static C1.m1(double)");
66+
}
67+
68+
void m1(int i) {
69+
System.out.println("Non-Static C1.m1(int)");
70+
}
71+
72+
void m2(int i) {
73+
System.out.println("Non-Static C1.m2(int)");
74+
}
75+
}
76+
77+
class C2 extends C1 {
78+
static void m1(double d) {
79+
System.out.println("Static C2.m1(double)");
80+
}
81+
82+
void m1(int i) {
83+
System.out.println("Non-Static C2.m1(int)");
84+
}
85+
86+
void m2(double d) {
87+
System.out.println("Non-Static C2.m2(double)");
88+
}
89+
90+
void m2(Integer ii) {
91+
System.out.println("Non-Static C2.m2(Integer)");
92+
}
93+
94+
void m3(int i) {
95+
System.out.println("Non-Static C2.m3(int)");
96+
}
97+
98+
void m4(Integer ii) {
99+
System.out.println("Non-Static C2.m4(Integer)");
100+
}
101+
}

0 commit comments

Comments
 (0)