Rob Prime wrote:You are absolutely right that using instanceof often will break transitivity. That's why I only use instanceof in two cases:
1) the class is final
2) the equals method is final
Seetharaman Venkatasamy wrote:I think you are worried about *symmetric* condition of equals .
It voilates Liskov substitution principle . because getClass pass the condition test, only if the two objects are belong to a same class.i.e, if you test whether some object of the subclass is equal to an object of the super class, it fails.
Rob Prime wrote:
But I must admit that this discussion has the potential to turn into a VI vs Emacs discussion.
They aren't called ancestors, but superclasses.
Gasan Gouseinov wrote: . . . this equals implementation isn't transitive when you have ancestors of this class?
Campbell Ritchie wrote:Yes, equalsIgnoreInheritance() could easily be implemented; it would not however have the symmetrical property of equals().
There are two good discussions I know about: one is in Joshua Bloch's Effective Java (you may be able to find the "sample" chapter for the 1st edition on the net still), the other will appear if you Google Angelika Langer Java equals.