Both methods have their problems.
If the subclass changes the identity, then you need to compare their actual classes. Otherwise, you violate the symmetric property. For instance, different types of Person
s should not be considered equivalent, even if they have the same name.
However, some subclasses don't change identity and these need to use instanceof
. For instance, if we have a bunch of immutable Shape
objects, then a Rectangle
with length and width of 1 should be equal to the unit Square
.
In practice, I think the former case is more likely to be true. Usually, subclassing is a fundamental part of your identity and being exactly like your parent except you can do one little thing does not make you equal.