• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
Whatever. Here's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic