ms_cs
I dont the definition of that method in super class...I want the reason for that answer. Then only I can understand the reason...
Ok , not a problem * Look like you are not clear about this
hashcode() concept * 😀
As such there is no super class that is directly inherited by
Person class(in Qn 1) so as it extends by default
java.lang.Object class Ok. Below is the definition of
hashcode() method in
java.lang.Object (done by Java guys ok , not me 😉 )
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the <tt>hashCode</tt> method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hashtables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java<font size="-2"><sup>TM</sup></font> programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
public native int hashCode();
Now though the above definition gives us gurantee that it will always return different
int value for two different objects and same
int values for one object called everytime but still we are given a facility to override this method in our own class(like
Person here) so that we can write efficient
hashcode() method based on our class and can make the searching process much faster(I think you are getting what i mean by searching here- searching in collections where actually
hashcode() is used 😀 )
Hope, I have given you now some idea. If still not and you are more curious to know about it, I would say go through this topic once from a good book. 😀
[ PS: A suggestion for you (Take it or Ignore it -Your wish)
If you want to stick to your points/answers while arguing, say from your side also rather than cutting off other points/answers everytime. 😀
]
Thanks