Reputation: 71
I was cleaning the code of the system on which I work, cleaning some Sonar tool issues, and I came across the following message:
Overide the 'equals' method in this class
I did some research but nothing that answered the "why" of this note
We are using a parent class with EqualsBuilder
which provides
EqualsBuilder.reflectionEquals
, so the correction is just to declare the method overwritten by passing the equals
method of the parent class
@Override
public boolean equals (Object o) {
return super.equals (o);
}
by guarantee I'm also overwriting the hashCode method, but in the same way passing the responsibility to the parent class (same case for reflectionHashCode HashCodeBuilder.reflectionHashCode
)
@Override
public int hashCode () {
return super.hashCode ();
}
But still my question remains, why do I have to override this method if it can be achieved in inheritance?
Thank you in advance
Upvotes: 3
Views: 5142
Reputation: 1539
Just a simple hack, since the super
Class has @Override
the equals
and hashCode
.
add it to class (if you are already using Lombok
):
@EqualsAndHashCode(callSuper = false)
unless the subclass class has its own equals
and hashCode
.
Upvotes: 1