Sub topic 13
Equality across subclasses: `instanceof`, `getClass`, and composition
This chapter assumes you know inheritance, method overriding, and the basic equals and hashCode contracts. The difficult part is deciding whether an object of a subclass should be equal to an object of its superclass.
Why inheritance makes equality difficult
Suppose Point compares only x and y. A subclass called ColorPoint adds a color field. Should new Point(1, 2) equal new ColorPoint(1, 2, "red")?
There is no universal answer. The choice must preserve the equals contract, especially symmetry:
a.equals(b)must produce the same result asb.equals(a).- Equal objects must have equal hash codes.
- Every field that affects equality must also affect
hashCode.
A superclass that accepts every subclass with instanceof can conflict with a subclass that needs to compare its extra fields.
The `instanceof` symmetry problem
An instanceof check treats a subclass as an instance of its superclass. That can be useful when subclasses do not add equality-relevant state. It becomes unsafe when they do.
class Point {
final int x;
final int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Point)) return false;
Point p = (Point) other;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return 31 * x + y;
}
}
class ColorPoint extends Point {
final String color;
ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ColorPoint)) return false;
ColorPoint p = (ColorPoint) other;
return super.equals(p) && color.equals(p.color);
}
@Override
public int hashCode() {
return 31 * super.hashCode() + color.hashCode();
}
public static void main(String[] args) {
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2, "red");
System.out.println(p.equals(cp)); // true
System.out.println(cp.equals(p)); // false
}
}The superclass ignores color, but the subclass compares it. Therefore symmetry fails.
Comparing with `getClass`
Using getClass() requires both objects to have exactly the same runtime class:
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Point p = (Point) other;
return x == p.x && y == p.y;
}This avoids the symmetry problem because a Point never compares equal to a ColorPoint. It is often the clearest choice for a final immutable value class. However, it prevents equality between subclasses even when their state is genuinely compatible.
| Strategy | Advantage | Risk |
|---|---|---|
| `instanceof` | Supports compatible subclasses | Subclasses can break symmetry |
| `getClass()` | Exact and predictable equality | Rejects all subclass comparisons |
| Composition | Avoids equality across the hierarchy | Requires separate contained objects |
Prefer composition for independent values
Composition means one object contains another instead of extending it. For example, a colored point can contain a Point and compare both the point and the color. This keeps each value type's equality rules independent.
Exercise
Why can a superclass using instanceof and a subclass using instanceof still violate symmetry?
Show answer
The superclass may compare only inherited fields, while the subclass also compares its new fields. The superclass can return true when the subclass returns false.