• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Override the equals-method

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am in the middle of an assignment and am stuck on: 'Override the equals-method (from Object) so that it returns true for two Circle objects that are logically equivalent based on the state of all of their instance variables.' The second circle object that is going to be compared to the first circle is provided by a driver (so I don't have it yet). So far my code for over riding the equals method looks like this:


I am new to Java and not really sure how to compare two objects, I am also wondering if I have to compare all of the variables related to the objects? Any help is appreciated. Thank you.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jackson Daniels wrote:ISo far my code for over riding the equals method looks like this:


And I'm afraid it's wrong, because it does exactly what Object.equals() (the method you're overriding) does.

I am new to Java and not really sure how to compare two objects,


Don't worry. You're not alone.

I am also wondering if I have to compare all of the variables related to the objects?


That's the general idea. The usual rule is that two objects - 'this' and 'obj' - are "logically equal" if
(a) They are the same type.
(b) All their critical values match.
(c) (if this is a subclass of something other than Object) super.equals(obj) is also true.

Tip: equals() is an oddly complex method, considering how often it's needed, and easy to get wrong.
I'd recommend reading this article if you can ... and if you find it too advanced right now, bookmark it and read it in a month or two, when you're ready, because it really is worth knowing about.

You'll find suggestions for how to code equals() methods everywhere. The only things I can suggest are:
1. Make 'if (this == obj)' your first test, and return true immediately if it's true.
2. Favour implementations that use instanceof rather than getClass(), even if they're a little tougher to follow.

HIH

Winston
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before you can write the method, you have to be able to explain in English or some other natural language what it means for your two "things" to be equal. If you are talking about circles, then perhaps just having the same radius makes them equal (or congruent, mathematically). Maybe they have to have the same radius and center. Maybe they have to have the same radius, center, and color. and thickness. and line type (dotted lines vs solid lines). the list could go on and on...

My point is, until YOU know what makes two circles/object equal, you can't write the method.
 
Slime does not pay. Always keep your tiny ad dry.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic