• 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

Override .equals

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I seem to have a issue I don't understand in the following application I override equals it doesn't seem to call the overridden version of equals in the code. It is still using the Object.equals version. My IDE which is netbeans says this is the source for the .equals code and it compiles fine and I have @Overrides statements . What am I missing here.

Thanks very much,



 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch Alexander. Can you please post your code inside the CODE tags. That will make it easier to read.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are missing here is that you were overriding the equals() method in the wrong class. Try overriding it in the Animal or Cat classes and see what happens.
 
Alexander Berry
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mean changing the code below from Animal to Cat the results are the same.

 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean move that code from ScjpOODemo class to the Cat class.

(Also, when you have time, read about overriding in a very good Java book, Head First Java.)
 
Alexander Berry
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done, it works. Thanks very much, now that I over that I can make the overrides a little more rock solid.

BTW, here is my Cat class.



Thanks again,

Alex.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcomed and I am glad it worked.

Definitely play around with your code to see what interesting variations you can make to it. What do you guess happens if the equals() method is moved from the Cat class to the Animal class?

Please continue to post questions when they come up.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexander Berry, Since you are invoking equals() method on instance of Cat (myCat). So you must override the equals() method in Cat class.
 
Alexander Berry
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much, I worked it out and with a previous post and figured it out.

Thanks ,

Alex.
 
reply
    Bookmark Topic Watch Topic
  • New Topic