Hi,
Nice to know that somebody takes the effort to clarify others doubts spending their precious time.
Thanks to Chandra Kota and Burkhard Hassel.
I think, now I have got it right. (corrections always welcome)
Reason 1:
As Burkhard Hassel said, we don�t want SETs to have meaningfully equal objects stored in our collection just because default hashCode says they are different objects.
Reason 2:
We use collections to store data and on a later date retrieve it back.
When (I don't override hashCode()) I store an element in a collection that uses hashing it will generate a unique hash and puts it in relevant bucket. Then when I want to retrieve it back I need to give something and whatever I give as my search criteria, the first thing the collection will do is take the newly generated hashCode(default) (not same as before and go to a different bucket) and then it will search for my object. It will not be found as the hashCode will be different for two different runs for the same object.
If I have overriden the hashCode based on data that is relavant or unique to the object, for different runs then it would still generate the same hashCode.
So it is mandatory to override hashCode for collections that use hashing.
Now after coming to the bucket there may be different objects with the same hashCode. Now to sort the exact match we cannot go with the default which also checks based on reference value. So we have to override the equals method also mandatorily.
Reason 3:
Another reason for maintaining the contract I believe is that, the default hashCode and equal method that comes with the API will be true for two equal objects which must not be changed when we override it in our programs.
Reason 4:
I have stored one object in collection. And in order to retrieve it (during the same execution) I am passing a meaningfully equal object (just as I have overriden my equals method to consider two objects to be equal). Now if I have not followed the contract my hashcode will be different for these two meaningfully equal object. So I will not have a successful search in collections that use hashing.
Rgds,
Paari.