I make mock exam, and I have question:
class A {
String a;
int c;
short b;
public int hashCode() { return a.length() * c;}
public boolean equals(Object o) {
//insert code
}
}
Insert code, that fulfill contract between equals and hashCode
And one from correct answer is:
((A)o).a.lenght() * ((A)o).b * ((A)o).c == this.code.lenght() * this.b * this.c;
I don't understand why...
If o1 has code="a", b=2, c=3 and
o2 has code="a", b=3, c=2 then
o1.equals(o2) == true, but o1.hashCode() != o2.hashCode !!!
Please, explain me why it's corect answer....