Many people don't like to use instanceof
, but I find that in many cases we have few other options when it comes to the equals
method. Take a look at the class below:
class A {
int n;
public A(int n) { this.n = n; }
@Override
public boolean equals(Object o) {
return false;
}
public boolean equals(A o) {
return n == o.n;
}
}
I've never seen something like this done, but could it serve as a replacement for having to use instanceof
to test if an Object
is an A
? Or are there other problems that I'm not thinking of?