Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Is overriding equals and hashcode absolutely necessary?

807588Mar 4 2009 — edited Mar 10 2009
Hi,

I've just off from the job interview, where I was asked about equals and hashcode, basically what are they. My reply was that they provide means of identity between class instances. To the question whether or not we need to override them, I said not always and that definitely upset my interviewer. He argued, that in real systems we HAVE TO override those methods ALWAYS. My point was, that unless you don't put the object in a Set and you can control the creation of new instances you don't have to override equals and hashcode.

What do you guys think? When you've created new entity class do you rush to override equals and hashcode right away? Or you wait till it becomes absolutely necessary?

Thanks for all your insights on this matter.
Ivan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2009
Added on Mar 4 2009
40 comments
337 views