...
Unfortunately, in this case it is impossible to extend the Card
class by adding a value or field in the subclass while preserving the equals()
contract. Use This problem is not specific to the Card
class, but applies to any class hierarchy that can consider equal instances of distinct subclasses of some superclass. For such cases, use composition rather than inheritance to achieve the desired effect [Bloch 2008]. This compliant solution adopts this approach by adding a private card
field to the XCard
class and providing a public viewCard()
method.
...
Bibliography
[API 2006] | |
Item 8. Obey the general contract when overriding equals | |
9.2, Overriding the | |
Chapter 3, Classes, Strings, and Arrays, The Object Class (Equality) | |
[Sun 2006] | Determining If Two Keys Are Equal (JCA Reference Guide) |
More Joy of Sets |
...