• Post Reply Bookmark Topic Watch Topic
  • New Topic

Override the equals-method  RSS feed

 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!