Possibly a dumb question, but I don\'t want to screw this up. Let\'s say I have two Java classes, Class1
and Class2
, where Class2 extends Class1
Effective Java does address this situation...by saying that you shouldn't do it. Item 8:
It turns out that this is a fundamental problem of equivalence relations in object-oriented languages. 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.
(Corollary: the same reasoning applies to hashCode().)