I've seen many implementations of the java equals() method which go along the following lines:
public boolean equals(Object other){
if (this == other)
return true;
//this if code
if (!(other intanceof MyClass))
return false;
//ends here
otherMyClass = (MyClass)other;
//check all the attribute of this and otherMyClass and return true or false
//accordingly
}
Isn't the code in if problematic in the sense that it will return true for o1.equals(o2) (with o1 objects of MyClass and o2 objects of subclasses of MyClasss) ? Which, in most of the cases, is not the expected behavior.
Wouldn't other.getClass() != this.getClass()
be a better comparison instead of the bolded if above ?