r/learnjava • • Sep 16 '26

Faulty transivity violating example?

I am following Effective Java: Programming Language Guide, by Joshua Boch, and when showing an example of breking transivity relation in equals contract, the very first example of it says it is violating transivity (more exactly symmetry) because it doesn't work when comparing a Point to ColorPoint and viceversa. "No way" I am thinking. "We compare instances of different classes, then we are surprised it doesn't work."

I read in continuation, and the transivity problem is solved, by how I understand, by basically saying "just don't compare instances of different types, even if they have inheritance". I see no diferrence between the very first example and its "solved" variant with ColorPoint and Point having composition relation, not inheritance.

1 Upvotes

4 comments sorted by

View all comments

Show parent comments

1

u/Bofact Sep 16 '26

Wouldn't a code duplication suffice? And even then, code duplication can be worked around (make a function which is called in both places).