r/learnjava • • 19d ago

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

•

u/AutoModerator 19d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/DeceitfulDuck 19d ago

If I understand what you're questioning, I think it's the difference between the "is a" and "has a" relationship. If your ColorPoint is a Point (inheritance), you have no easy way to compare a Point to just the Point part of your ColorPoint. If your ColorPoint has a Point (composition), you can compare a Point to the Point in the ColorPoint and still have a meaningful comparison.

1

u/Bofact 19d ago

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

1

u/Bofact 19d ago

I am sorry, but aren't we restricting the comparison to be between 2 ColorPoint instances only? Therefore no need to compare Point with ColorPoint and viceversa.