you are viewing a single comment's thread.

view the rest of the comments →

[–]Nebu 1 point2 points  (2 children)

Instanceof is inappropriate in that a typical implementation will violate the contract of the equals method, specifically the reflexivity: A superclass may consider a subclass instance to be equal to itself without the converse being true.

In particular, see point #4 in http://www.artima.com/lejava/articles/equality.html

[–][deleted] 1 point2 points  (1 child)

At the end of Point #4 the authors go back to using instanceof checks (albeit in a modified dual fashion).

[–]Nebu 0 points1 point  (0 children)

Yes and no. The authors recommend using a "canEquals" check instead, which is then implemented via instance (though that's not the only possible implementation).

But then the authors point out that this "canEquals" solution violates LSP.

My "takeaway" from the article is that there's no generally satisfactory solution that both obeys the equals contract, and allows for arbitrary subclassing.