• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Overriding equals()

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to understand 'Overriding equals()' topic from K&B chapter 7.
But I am not able to understand it properly. Can anyone please suggest me some other link which can explain the same in simple language

Thanks
Aparna
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aparna ,
What I can suggest you that,you just take one example of overriding equals() and then try to understand it .If you are not clear then ask for the explanation on those point you are not clear.
regards,
Subhasish
[ November 18, 2008: Message edited by: subhasish nag ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals() method is the way to determine when two objects are considered equals based on real caracteristics that makes sense to the class writer.

When you compare objects, if they dont override equals, how can Java know if they are equal? Java can't guess it.

If you don't provide a way for Java to know if they are equal, Java will compare its references, and references are barely "memory address", which usually doesnt make sense in a real world. Thats what the Object equals() method do, compare the reference of two objects with "==".

For instance, the String equals() method was overriden, and for the String's Class writer, it makes sense to say that two String objects are equals if they have the same character sequence.

If you write a Client class, and for your business logic two Client's are equals when they have the same account number, you can override the equals() method in your class and return true when the objects account number are the same, which means "For the Client class, it makes sense to say that two instance of Clients objects are equals when they have the same account number".

Hope it helps.
 
Aparna Misri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Fabio
reply
    Bookmark Topic Watch Topic
  • New Topic