This standard implementation. The interface implies that any object can get into the equals()
method.
Accordingly, we first need to find out whether the test makes sense: if this object is null
or of a different type, then we are definitely not equal to it ( return false;
).
After we figured out that the transferred object is of the type we need, we can compare the fields. But we are given a parameter of type Object
, and in it the fields a
and b
missing. Therefore, we reduce it to our type A
( A other = (A) obj;
), and a new object is not created. And now we can refer to the fields a
and b
.
PS True, the comparison logic is strange. The idea should be like this:
return this.a == other.a && this.b == other.b;
a
andb
typeInteger
, for example, then it will be fully compiled. Although the logic of comparison is still strange. - andreychagetClass()
leads to violation of the LSP, on the other hand, usinginstanceof()
leads to violation of the equalityx.equals(y) == y.equals(x)
. - andreycha