posted 16 years ago
This one is another proof by contradiction.
The question:
The answer claims that two do:
C:
The above is fine and will fulfill the contract.
D
Of course, the contract basically boils down to, "when o1.equals(o2) is true, o1.hashCode() == o2.hashCode() must also return true.
The above is claimed to be correct, but actually FAILS to fulfill the contract, because it will return true at times that hashCode() will return false. For instance, whenever rate and bal are flipped between the two objects being compared. See the following:
If executed, the above will print "Equals returns true but hashCode returns false". Therefore, D does not fulfill the contract and should be considered an incorrect response.