I read up on my subject heading a little bit more, and I just want to make sure I understand this correctly. It seems like the answer is that we still need to override equals on the class to define what the structural equality check is? So in other words if we want to check the customer1 == customer2
then we first have to define what structural equality is for Customer
by implementing equals, and at that point Kotlin will use our implementation by means of the ==
operator?
So in this case if Customer
inherits from Person
and we also want perform a structural equality check using properties from Person
then we would implement that in the equals(Object object)
method?
So in other words if we want to check the customer1 == customer2 then we first have to define what structural equality is for Customer by implementing equals, and at that point Kotlin will use our implementation by means of the == operator?
Yes. If Customer
is a data class
, then you get the implementation of equals
automatically, otherwise you need to define it explicitly.
So in this case if Customer inherits from Person and we also want perform a structural equality check using properties from Person then we would implement that in the equals(Object object) method?
Again, yes (use Any?
instead of Object
), just like you would in Java, and it's... complicated. See https://stackoverflow.com/a/7798706/9204 and https://stackoverflow.com/a/13163898/9204, and the articles linked from them, for more.
I'll add that Kotlin's usage of the term "structural equality" is not the normal one. It's usually reserved for the cases where equality is checked by comparing all (relevant) fields for equality like in data class
, not "whatever equals
happens to do".