• 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

What it means if you don't override equals() ? [Preparing for OCP]

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone, I am preparing for OCP and I am not able to understand the context here.. Can someone help me to understand this.

What it means if you don't override equals() ?... Of course, overriding the equals() method for Car also allows the potential for more than one
object representing a single unique car to exist, which might not be safe in your design.



Page #578
OCA/OCP Java® SE 7 Programmer I & II Study Guide
(Exams 1Z0-803 & 1Z0-804)

Thanks in advance.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means that when you decide to override equals(), think hard about what the consequences are to the semantics of equality.

For example, if your Car had a VIN property, which is unique for each and every car, then think twice before using VIN as part of the definition of equals(). Because that would mean that this is possible:

This could be fine but it could also cause some unwanted surprises elsewhere in your code. You need to make sure there are no surprises.
reply
    Bookmark Topic Watch Topic
  • New Topic