Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

override equals

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • 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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or simply
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic