If you override the equals() method in a class, you must also override the hashCode() method to avoid violating the general contract for hashCode. The default hashCode() implementation returns an integer based on the object's identity, not its field values. So two equal objects according to the overridden equals() method could return different hash codes and fail when used with hash-based collections like HashMap. Overriding hashCode() to consider the same fields checked in equals() fixes this issue.