• 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

Overriding .equal method to compare between 2 objects (user defined classes)

 
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello CR,

How is everybody today ?

Guys, I'm having a little bit of a challenge overriding the .equal method available in java.lang.object, it's driving me crazy and I can't do it, please check my work and advise me =)

I have 2 classes and an Interface as following :







What's required from me at this point is :

The class overrides equals(Object o) in java.lang.Object to compare the instance values of two Pixel objects and return true if they are equal, and false otherwise.



Also, in the same so called equal method, when I tried to simply write the code like this :


I got an error saying that the method must return a boolean value !!
I had to declare a boolean variable and use it, and the error was still appearing until I added the else statement, so why it didn't just accept the return true; statement ?

I hope this is not too much








Thanks,
Sarah
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that's the only change you made to your code? Because you definitely wouldn't get an error just replacing that with return true. But you would if, for instance, you weren't returning a value in all paths.

The other immediate issue is that you're being asked to override equals(Object), but instead you've added a new method equals(int x, int y, Pixel p) - so you're overloading rather than overriding.
 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals method is not overridden, but is overloaded. I guess you are confused between overriding and overloading. For overriding a method,the important rule you need to follow is : The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded method you didn't intend. You can modify your equals method with the one below if you need a overridden equals method:
 
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, the equals method is not overriden properly, because it must have an Object as it's argument and not int, int, Object. (Edit: see Vivek's post)

See here for the equals method signature and details:
equals() method

Also, when you have a method that must return a value (in our case the equals method must return a boolean value), you must make sure that it will return something. Your method, before you put the else clause, I guess must have been like this (in pseudocode):


As you can see, the method will return something only when we get inside the if condition. But what happens if we dont? Then we we will not return anything and that is why the compiler is complaining. When you put the else clause with a return statement in it, the compiler knew that either way, this method will return something. So, everything is ok and you don't need to create an additional variable because that's reduntant.


 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everybody, I got it working now because of your help =)
This is how it looks now if you're interested :

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're still overloading rather than overriding, though. Try adding an @Override annotation and that will become clear. The signature needs to be equals(Object), not equals(Pixel). You should be able to compare a Pixel to any object (returning false if it isn't a Pixel). It also needs to cope with being compared to null (again, returning false).
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:You're still overloading rather than overriding, though. Try adding an @Override annotation and that will become clear. The signature needs to be equals(Object), not equals(Pixel). You should be able to compare a Pixel to any object (returning false if it isn't a Pixel). It also needs to cope with being compared to null (again, returning false).



Oops o_O
I thought it's supposed to be like this :S

So, now when I changed the argument to (Object o) I got an error for all the instances I have (x, y and myColor) !!!
How can I resolve this ??

I don't know how to use the @Override thingy =(
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote:Hello CR,

How is everybody today ? . . .

I am very well, thank you. So are the rest of the family.

You need to find the standard references about the equals method Bloch, Langer and Odersky Spoon & Venners. The good news is, they will tell you all about the equals() method. The bad news is, you find out how difficult it is to write.
 
Panagiotis Kalogeropoulos
Rancher
Posts: 99
Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, now when I changed the argument to (Object o) I got an error for all the instances I have (x, y and myColor) !!!
How can I resolve this ??



You must cast the Object that you have as argument to Pixel, using (Pixel) object. Check Vivek's post to see how this is done.

I don't know how to use the @Override thingy



The @Override is an annotation (see here for more details: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html) and you put it above the method that you override.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote: . . . I don't know how to use the @Override thingy =(

Try here.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use an @Override annotation just by adding it on the line above the method signature:
What you're doing is telling the compiler "I am intending to override a method here". The advantage of this is that the compiler will then tell you if in fact you aren't overriding one - for example, if you've just spelled the method wrong, or have the wrong arguments. This sort of error can be really hard to track down - your code might be perfect otherwise, but your method just never gets called. So turning it into a compiler error saves a lot of time pulling your hair out.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit - ah, too slow - I was looking for a good link for describing the equals() method in full, but Campbell's already given one.
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uffff, this toke a lot of my time, although your inputs were crystal clear but I just couldn't see it at that time =(
Plus I think this is the first time I actually do ( object casting ) !

Anyways, here is my final code, it's working just fine. Thank God !






Thank you guys, I'm really glad that all of you were nice enough to help me out. Appreciated =)






Sarah,
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That equals() method will work reliably as long as you cannot make subclasses of Pixel.
 
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

Sarah Mitchell wrote:Uffff, this toke a lot of my time, although your inputs were crystal clear but I just couldn't see it at that time =(
...
Thank you guys, I'm really glad that all of you were nice enough to help me out. Appreciated =)


For future reference, here's a pattern you can use for pretty much any equals() method you write:In particular, remember the final qualifier.

Unless you need to write different equals() methods for each member of a hierarchy of classes (and that has its own problems, which probably aren't worth worrying about right now), the above template will work just fine.

HIH

Winston
 
Winston Gutkowski
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

Campbell Ritchie wrote:That equals() method will work reliably as long as you cannot make subclasses of Pixel.


Actually, it will work even if you can; providing none of those subclasses add attributes that affect the equals method.

Then you're into a whole different ball of wax anyway; and any solution is going to have its problems.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic