This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Reason for Override!  RSS feed

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings and wrappers,Hashtable,HashMap override equals(),hashcode()
What is the reason for Override these methods?
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphic behavior.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Everybody's invited. Except this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!