aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() contract problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() contract problem" Watch "equals() contract problem" New topic
Author

equals() contract problem

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
[Added code tags - see UseCodeTags for details]
does this equals()override fulfills method?
Please reply as soon as possible.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

What happens if o isn't an instance of A?
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Sep 10, 2012
Posts: 70
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() contract problem