The answer given is C & D. but in D, isn't it possible for the hashcode to be not equal, and cause equals() to return true? Isn't that a violation of the contract? eg:- Sortof1 - code.length() = 2 bal =3 rate = 4 Sortof2 - code.length() = 3 bal = 4 rate = 2
No I don't think exam question will treat this as an error. There are questions like
Assume that XYZ class implements equals and hashCode correctly. Then what will happen when you run this code
or like this
what will happen when you run this program
and the program will not fulfill the contract so the output will be unexpected. But this doesn't mean that it will be an error. You have to choose the correct output which will come when the contract is broken..
Joined: Nov 10, 2008
ya, but what if it's a direct question?
if x5.equals(x6) == true , then is x5.hashcode() == x6.hashcode() will "always" be true ?
without saying anything about whether the contracts are followed