Say I have a class where I would have to check for multiple different types of objects. Would it be feasible/possible to override equals() in the following manner?
public boolean equals(Object o){
if(o == null) return false;
if(o instanceof class1){
return this.class1.equals((class1) o);
if(o instanceof class2){
return this.class2.equals((class2) o);
...
}
Would this ever be useful? I assume here that I create a static overloaded equals() method in the respective classes (though maybe polymorphism would take care of that automatically as long as I cast?).
It seems like you want a method to check whether a specified object is equal to at least one of various member variables in the receiver.
It is inappropriate to override java.lang.Object#equals for this purpose.
You should instead provide a different method, e.g.