`equals` method inherits parent class implementation instead of overriding `Object.equals`JAVA-E0099
This class defines an equals
method that doesn't override Object.equals(Object)
. In addition, it inherits an overridden equals(Object)
method from a superclass. This may lead to unexpected results when comparing instances of this class.
Bad Practice
class Parent {
String id;
@Override
public boolean equals(Object other) { return other instanceOf Parent && this.id.equals(((Parent)other).id); }
}
class Child extends Parent {
String name;
public boolean equals(Child other) {
return this.name == other.name && this.id == other.id;
}
}
Here, Child
inherits an overridden definition of equals
from Parent
and also defines an overloaded version of equals
taking a Child
as an argument. Any standard library collection of Child
will use Parent
's definition of equals
instead of the intended overloaded version in Child
, ignoring the name
field completely.
Recommended
It is recommended to explicitly override the default equals(Object)
method to make things clear, and also prevent any subtle logic bugs if equals
is required to behave differently in the child class.
By not doing so, equality checks for this class may no longer be symmetric or transitive.
public boolean equals(Child other) {
return this.name == other.name && this.id == other.id;
}
public boolean equals(Object other) {
if (other instanceof Child) return this.equals((Child)other);
else // ...
}
References
- SpotBugs - EQ_OTHER_NO_OBJECT