I saw a colleague write it as follows:
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ImageCode other = (ImageCode) obj;
if ((this.code == null) ? (other.code != null) : !this.code.equals(other.code)) {
return false;
}
return true;
}
Then, I suggested changing it for this:
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Parent)){
return false;
}
final ImageCode other = (ImageCode) obj;
if ((this.code == null) ? (other.code != null) : !this.code.equals(other.code)) {
return false;
}
return true;
}
but he told me that it is wrong, and that instanceof
can't be used for the equals
method. I don't understand why he said that.