artima - The Trouble with Checked Exceptions artima.com - get the latest breaking news, showbiz & celebrity photos, sport news & rumours, viral videos and top stories from artima.com Daily Mail and Mail on Sunday newspapers.
How to Write an Equality Method in Java
by Martin Odersky, Lex Spoon, and Bill Venners
June 1, 2009
equals method that preserves the contract of
equals even when subclassses of concrete classes add new fields.
In Item 8 of
1, Josh Bloch describes the difficulty of preserving the
equals contract when subclassing as a “fundamental problem of equivalence relations in object-oriented languages.” Bloch writes:
There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction.
Chapter 28 of
Programming in Scala shows an approach that allows subclasses to extend an instantiable class, add a value component, and nevertheless preserve the