• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding equals (was "Generics")

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confuse about overriding the equals()method.
Why we need (o instanceof moof)(see page no:527 of K & B for SCJP- 1.5 exambook)?
Can anybody help me?
I am stuck at that point.

Edit by mw: Changed subject line because this question is not about generics.
[ June 18, 2007: Message edited by: marc weber ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

The equals method is defined in the Object class to take an Object as a parameter. When calling equals, any type can be passed as a parameter because the reference will be converted (upcast) to type Object.

So when overriding equals, you need to verify that the true type of the other object is what you expect it to be. Otherwise, you could run into problems when you try to access fields or methods using that reference.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why you need to override equals() for key? page:525.
Can anybody explained?
Thanks.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
Why you need to override equals() for key? page:525...


I don't have the book, so I don't know the context.

But because it's called "key," my guess is that it is being used in a Map (which stores key/value pairs). In a Map, keys must be unique because they are used to lookup the associated value. Therefore, this class would need a meaningful implementation of equals to ensure that the Map is not accepting "duplicate" keys.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.But which you have explained that I understand. But if you dont have book then you are no able to explained me.

Thanks again.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct explanation, Marc, infact an accurate guess
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic