Is overriding equals and hashcode absolutely necessary?
807588Mar 4 2009 — edited Mar 10 2009Hi,
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