Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() contract problem

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags - see UseCodeTags for details]
does this equals()override fulfills method?
Please reply as soon as possible.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if o isn't an instance of A?
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:What happens if o isn't an instance of A?


Yes that's what I am thinking. Because according to me it does not clear the "NULL" point of the contract and so it does not fulfill the contract for equals() method.
BUT, in practice exam4, question 6, it is given that it does as answer.
So please clarify.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you're right, there's an error in the practice exam. That method is fine for the cases where o is a non-null A object, but it will throw a NullPointerException if o is null. The contract java.lang.Object#equals(java.lang.Object) says:

- For any non-null reference value x, x.equals(null) should return false.


It will also throw a ClassCastException if o is a different type, which also doesn't meet the statement "returns true if this object is the same as the obj argument; false otherwise".
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic