File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reason for Override! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reason for Override! " Watch "Reason for Override! " New topic
Author

Reason for Override!

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Strings and wrappers,Hashtable,HashMap override equals(),hashcode()
What is the reason for Override these methods?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Polymorphic behavior.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Arjun Cheng
Greenhorn

Joined: Jan 08, 2008
Posts: 13
equals and hashcode are methods from Object. Collection famework class like HashTable, TreeSet, ... when you put objects in to collection class as mentioned before. you need overriden equals and hashcode methods. If not they won't do the thing like you thought.
Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
Hi

"Strings and wrappers,Hashtable,HashMap override equals(),hashcode()
What is the reason for Override these methods? "

we have to reframe the sentence :
Strings and wrappers over-ride the equals and hascode method
and HashMap and HashTable require these methods to be over-ridden by the class ( only if calss is user-defined not in case of Strings and wrappers as they already over-ride them )whose objects we are going to add them beacuse Map stores key-value pairs , to access them or retrieve them , addreses of keys ( which we get thru hashcode ) and their corresponding values ( which we get thru equals method)so over-ridding is must .

Now the imp thing why in Strings and Wrapper classes these methods were overidden ? Because : Mother class Object doesnt provide the required implementation of equals method i.e to compare two objects thru thier states or say values.

Hope this makes sense.

Thanku
Vishal..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Reason for Override!
 
Similar Threads
About servlet's service() method
Regarding Service, SevletConfig and ServletContext
StringBuffer doubt
User-defined Exception class
overridding service()