why should we over ride hashCode(), when equals was over ride()?

why should we over ride hashCode(), when equals was over ride()?

why should we over ride hashCode(), when equals was over ride()?

View Answers

June 2, 2011 at 12:22 PM

In Java, every object has access to the equals() method because it is inherited from the Object class. However, this default implementation just simply compares the memory addresses of the objects. You can override the default implementation of the equals() method defined in java.lang.Object. If you override the equals(), you MUST also override hashCode(). Otherwise a violation of the general contract for Object.hashCode will occur, which can have unexpected repercussions when your class is in conjunction with all hash-based collections.









Related Tutorials/Questions & Answers:

Ads