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.
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".