RFR: 1542: Census Namespace missing hashCode and equals [v2]

Joe Darcy darcy at openjdk.org
Thu Aug 11 22:52:23 UTC 2022


On Thu, 11 Aug 2022 21:36:33 GMT, Erik Joelsson <erikj at openjdk.org> wrote:

>> Hm, I let the IDE generate these for me so didn't think much of it. Aren't there subtle differences with instanceof, if there are subtypes involved? Not that it matters in this particular instance.
>
> So I experimented some more. In the intellij wizard for this, if I check "accept subclasses", it generates code with `instanceof` instead. I don't think this equals method should do that.

To circle back on this, there is a school of thought that getClass should not be used in this fashion in an equals method since it violates the Liskov substitution principle -- instances of a subclass can be used where instances of the superclass can used. "Effective Java, 3rd edition" covers this topic in item 10. However, in a case like this where (I assume) subclasses are not expected, it doesn't matter very much in practice.

-------------

PR: https://git.openjdk.org/skara/pull/1356


More information about the skara-dev mailing list