Saturday, August 28, 2010

equals() method must take an Object

Courtesy of Sun Web Learning Center

If a class X correctly overrides equals() and hashCode(), and if a and b are valid references to instances of class X, which two are true? (Choose two.)

A If a.equals(b) is false then b.equals(a) is ALWAYS false.
B If a.hashCode() == b.hashCode() then a.equals(b) is ALWAYS true.
C If b.equals(a) is true, then a.hashCode() is ALWAYS == to b.hashCode().
D If a.equals(b) is false, then a.hashCode() will NOT be == to b.hashCode().
E The class's equals method could be:
public boolean equals(X myX) {
return y.equals(myX.y);

A and C are correct. Option E is incorrect because the equals() method must take an Object.

No comments:

Post a Comment