I have some doubts about equals
and hashCode
contract in Java using EqualsVerifier library.
Imagine we have something like this
public abstract class Person {
protected String name;
@Override
public boolean equals(Object obj) {
// only name is taken into account
}
@Override
public int hashCode() {
// only name is taken into account
}
}
And the following extended class:
public final class Worker extends Person {
private String workDescription;
@Override
public final boolean equals(Object obj) {
// name and workDescription are taken into account
}
@Override
public final int hashCode() {
// name and workDescription are taken into account
}
}
I try to test whether I fulfill the equals
and hashCode
contract in the Person class, using EqualsVerifier
@Test
public void testEqualsAndHashCodeContract() {
EqualsVerifier.forClass(Person.class).verify();
}
Running this test, I get that I have to declare equals
and hashCode
methods final, but this is something that I don't want to do, because I may want to declare these two methods in the extended classes, since I want to use some child's attributes in equals
and hashCode
.
Could you skip for testing the final rule in the EqualsVerifier library? Or am I missing something?