• 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

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a class myself , I want to override equals method in it.



error is missing return statement.

Did I override equals correct?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clearly if you get an error while compiling, you did not do something correct. Your primary error here is that there are ways that you can go through your code and never hit a return statement.

What happens if 'this' does NOT equal 'obj', and your obj IS an instance of equalsTest?

you'd skip your first if statement, skip the second one, and have nothing to return.
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are getting the missing return statement. Why ? If for some reason the execution reaches the last if(-----) AND the condition inside that if evaluates to false then no value is returned. Suppose there was an else with the last if() and it returned a boolean, that error will go.

PS : Please give a short description of your code. It is easier to go through the code after reading some info rather than figuring out on my own.

Here is a remedy for such a situation :



That should make the compiler happy.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:


If obj == this then a == this and a.number == this.number per definition. You can replace the entire block by this:
The rest of the functionality, testing for number equality, should be done if the instanceof operator returns true - the part you're missing right now.
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or simply
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


At first I must be sure these objects has same type, Then check their fields

if this == obj , then equalsTest a=(equalsTest)obj; if a.number == this.number return true. Right?

But it has error, equal method does not check type of its parameter.for this line:



and has error for this:


 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Byron wrote:or simply


Which completely ignores checking for actual equality. You might as well not override equals and inherit the method from Object, which does the same.

Abalfazl, I think you want something like this:
 
reply
    Bookmark Topic Watch Topic
  • New Topic