Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions why should we over ride hashCode(), when equals was over ride()?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


venkata santosh
why should we over ride hashCode(), when equals was over ride()?
1 Answer(s)      3 years and 7 months ago
Posted in : Java Interview Questions


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


Advertisement
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:
 

 

 

DMCA.com