is overwritten due to data classes' characteristics. The difference is, that in the example
equals()
is based solely on
id
and
hashCode
uses
name
and
id
, both.
p
Paul at Avans Breda
09/10/2024, 11:18 AM
But in this example it is possible that two objects are equal (using the overridden equals() method) but have a different hashCode.
Am I correct that this violates the contract between equals() and hashCode(): objects that are equal to each other must return the same hashCode
r
Ronny Bräunlich
09/10/2024, 11:20 AM
That's correct. It violates:
_equals consistency_: objects that are equal to each other must return the same hashCode
p
Paul at Avans Breda
09/10/2024, 11:21 AM
For an example that is looked upon by many students learning the Kotlin language this is confusing.