-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEqualsUsesInstanceOf.java
More file actions
70 lines (58 loc) · 1.48 KB
/
EqualsUsesInstanceOf.java
File metadata and controls
70 lines (58 loc) · 1.48 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
63
64
65
66
67
68
69
70
class BadPoint {
int x;
int y;
BadPoint(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if(!(o instanceof BadPoint))
return false;
BadPoint q = (BadPoint)o;
return x == q.x && y == q.y;
}
}
class BadPointExt extends BadPoint {
String s;
BadPointExt(int x, int y, String s) {
super(x, y);
this.s = s;
}
// violates symmetry of equals contract
public boolean equals(Object o) {
if(!(o instanceof BadPointExt)) return false;
BadPointExt q = (BadPointExt)o;
return super.equals(o) && (q.s==null ? s==null : q.s.equals(s));
}
}
class GoodPoint {
int x;
int y;
GoodPoint(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (o != null && getClass() == o.getClass()) {
GoodPoint q = (GoodPoint)o;
return x == q.x && y == q.y;
}
return false;
}
}
class GoodPointExt extends GoodPoint {
String s;
GoodPointExt(int x, int y, String s) {
super(x, y);
this.s = s;
}
public boolean equals(Object o) {
if (o != null && getClass() == o.getClass()) {
GoodPointExt q = (GoodPointExt)o;
return super.equals(o) && (q.s==null ? s==null : q.s.equals(s));
}
return false;
}
}
BadPoint p = new BadPoint(1, 2);
BadPointExt q = new BadPointExt(1, 2, "info");