In the class above the equals() method has been overridden to make two objects of the same class to be considered meaningfully equal. The hashcode() method has Not been overridden, thats perfectly legal....The hashcode() method matters only if you want to put these objects of these classes into a collection and want to retrieve them...
Lets say for example
You have a HashMap collection in which you want to put objects of type TestTwelve(the above mentioned class)..here is the code
HashMap h=new HashMap();
h.put(new TestTwelve(3),new TestTwelve(5));//1
h.put(new TestTwelve(5),new TestTwelve(6));//2
//You try to retrieve the above objects as below
h.get(new TestTwelve(3));
h.get(new TestTwelve(5));
But the result will be rubbish since you cannot retrieve the objects which you inserted on lines1 and 2
So the thing is hashcode() and equals() must be overridden only if you are using your class as a key in a hashmap or any other collection class that uses hashcodes.
The code above is just comparing the two objects and saying its equal.It has nothing to do with collection classes... The other thing in the code is the 2 hashcode values returned are different and so these two objects land in different buckets(when collection is involved)
The contracts matter if collections are involved
[ August 21, 2008: Message edited by: Thirumalai Muthu ]
[ August 21, 2008: Message edited by: Thirumalai Muthu ]