It seems that ExtendedGoodOne#equals does not violate the contact. It was generated using standard ide plugin.
And we can test it using Set. The following example shows, that GoodOne and ExtendedGoodOne can't be in one Set together (if we change order of addition).
But instances of ExtendedGoodOne and its subclass - can.
Code: Select all
public static void main(String... strings) {
GoodOne x = new GoodOne();
GoodOne y = new ExtendedGoodOne();
// symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
System.out.println(x.equals(y) + " " + y.equals(x)); // output: true, false
// add instances of GoodOne and ExtendedGoodOne to one Set
Set<GoodOne> set1 = new HashSet<>(), set2 = new HashSet<>();
set1.add(y); set1.add(x);
set2.add(x); set2.add(y);
System.out.println(set1.size() + " " + set2.size()); // output: 1, 2
// add instances of ExtendedGoodOne and its subclass to one Set
GoodOne z = new ExtendedGoodOne() {}; // not an instance of ExtendedGoodOne, but an instance of anonymous class, which extends ExtendedGoodOne
System.out.println(z.getClass() == x.getClass()); // output: false
Set<GoodOne> set3 = new HashSet<>(), set4 = new HashSet<>();
set3.add(y); set3.add(z);
set4.add(z); set4.add(y);
System.out.println(set3.size() + " " + set4.size()); // output: 1, 1
}
So, it seems there is nothing wrong with equals of ExtendedGoodOne, but something wrong with GoodOne#equals. It would be better, I think, to prohibit extending of GoodOne to be sure that there is no doubt about сorrectness of its equals method from this point of view.
By the way, the standard ide plugins offers comparison by 'instanceof' instead of comparison by 'getClass()'.
It seems this is also due to violation of symmetric while overriding.