Sub topic 12
Overload versus override: a common `equals` bug
Prerequisites and the core mistake
This chapter assumes that you can write Java classes, use collections such as List, Set, and Map, and understand method overriding.
A method override replaces an inherited method with the same name and parameter types. A method overload uses the same name with different parameter types. Java collections call equals(Object), so an overload such as equals(Id) does not replace Object.equals(Object).
The bug: `equals(Id)` is an overload
Consider this class:
import java.util.List;
class Id {
private final int value;
Id(int value) {
this.value = value;
}
// This is an overload, not an override.
public boolean equals(Id other) {
return other != null && value == other.value;
}
}
public class Main {
public static void main(String[] args) {
Id first = new Id(1);
Id second = new Id(1);
System.out.println(first.equals(second)); // true
List<Object> ids = List.of(first);
System.out.println(ids.contains(second)); // false
}
}The direct call first.equals(second) can select equals(Id), so it returns true. However, List.contains works through the Object type and calls equals(Object). Since Id did not override that method, it inherits Object.equals(Object), which compares references. The two Id objects are different objects, so the result is false.
The parameter type must be exactly Object:
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Id)) {
return false;
}
Id id = (Id) other;
return value == id.value;
}The @Override annotation asks the compiler to verify that a method really overrides an inherited method. With equals(Id), compilation fails because no inherited method has that signature.
Which method does Java call?
The compiler chooses an overloaded method from the expression's declared type and the argument types. At runtime, Java chooses an overridden method from the object's actual class.
For equality, the public method should normally be exactly:
@Override
public boolean equals(Object other)Do not add only a more specific overload. If a convenience overload is useful, it may coexist with the real override, but it must not replace it.
A complete corrected class
import java.util.Objects;
final class Id {
private final int value;
Id(int value) {
this.value = value;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Id)) {
return false;
}
Id id = (Id) other;
return value == id.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return "Id[" + value + "]";
}
}
public class Main {
public static void main(String[] args) {
System.out.println(java.util.List.of(new Id(1)).contains(new Id(1)));
}
}The hashCode override is required because equal objects must have equal hash codes. Without it, a HashSet or HashMap can still fail to find an equal Id.
Exercise
What is wrong with public boolean equals(User other), and which declaration should replace it?
Show answer
It is an overload because its parameter is User rather than Object. Replace it with @Override public boolean equals(Object other).