1.

When You Are Writing Equals() Method, Which Other Method Or Methods You Need To Override?

Answer»

Hashcode is the right answer. Since EQUALS and hashCode have their contract, so overriding one and not other will break the contract between them. By the way this question can lead to an interesting discussion, if Interviewer likes to go on deep e.g. he MAY ASK what are those contracts, what happens if those contracts break 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 need to be consistent with equals, which is another interesting question on our list.

Hashcode is the right answer. Since equals and hashCode have their contract, so overriding one and not other will break the contract between them. By the way this question can lead to an interesting discussion, if Interviewer likes to go on deep e.g. he may ask what are those contracts, what happens if those contracts break 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 need to be consistent with equals, which is another interesting question on our list.



Discussion

No Comment Found