...
By operating on String
objects, the CaseInsensitiveString.equals()
method violates the second contract requirement (symmetry). Because of the asymmetry, given a String
object s
and a CaseInsensitiveString
object cis
that differ only in case, cis.equals(s))
returns true
while s.equals(cis)
returns false
.
...
In the noncompliant code example, p1
and p2
compare equal and p2
and p3
compare equal but p1
and p3
compare unequal; this violates the transitivity requirement. The problem is that the Card
class has no knowledge of the XCard
class and consequently cannot determine that p2
and p3
have different values for the field type
.
Compliant Solution
Wiki Markup |
---|
Unfortunately, it is impossible to extend an instantiable class (as opposed to an {{abstract}} class) by adding a value or field in the |
equals()
contract.Compliant Solution
Wiki Markup |
---|
It is impossible to extend an instantiable class subclass while preserving the {{equals()}} contract;. It is therefore recommended to use composition rather than inheritance to achieve the desired effect \[[Bloch 2008|AA. Bibliography#Bloch 08]\]. This compliant solution adopts this approach by adding a private {{card}} field to the {{XCard}} class and providing a {{public}} {{viewCard()}} method. |
...