While reviewing the questions I had wrong on the
exam on the cd that comes with the K&B book (
test A,
I don't see the question number), I do not understand
one of the answers.
The question is to select all lines that will fulfill the
equals() and hasCode() contracts for this class.
My answer was:
C: check whether product of code.length() and bal is equal.
But MasterExam said also D is correct:
D: check whether product of code.length(), bal and rate is equal.
The answer further explains that the equals() method must be as least
as precise as the hashCode() method is.
I don't understand (maybe I should go to sleep
My problem is the following:
Suppose I have two objects, which are equal because they
both have rate == 0. Those objects can easily have different
hashCodes. Hence, the hashCode contract is violated, right?