top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

When you are writing equals() method, which other method or methods you need to override?

+1 vote
360 views
When you are writing equals() method, which other method or methods you need to override?
posted Dec 29, 2015 by Shyam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Since equals and hashCode has there contract, so overriding one and not other, will break contract between them. By the way this question can lead on interesting discussion, if Interviewer likes to go on deep e.g. he may ask about what are those contracts, what happens if those contracts breaks etc. I like to give an example How equals and hashcode are used in hash based collections e.g. Hashtable, that leaves positive impression more often. You can also mention about compareTo() here to score some additional point, this method should also needs to be consistent with equals, which is another interesting question in our list.

answer Dec 30, 2015 by Karthick.c
...