I tried to override equals and hashcode methods in a class. It is a subclass of another class which does not implement the equals method and hashCode methods.
Eclipse gave the below warning .
The super class ABC does not implement equals() and hashCode() methods.
The resulting code may not work correctly.
Why is the above warning given ? Under what circumstances it may not work correctly ?
If you say
a.equals(b)versusb.equals(a)it is reasonable to expect the behaviour to be the same. But if they are of corresponding typesBandArelated by inheritance and only one of them properly implementsequalsthen the behaviour will be different in those two examples.Here,
Ais the superclass and does not implementequalsat all (so it inheritsjava.lang.Object.equals). SubclassBoverridesequalsto depend on thenamefield.Unsurprisingly, the output is
false, thus breaking symmetry.