Because a ClassB object is a ClassA object. You can always, anywhere, treat a ClassB object as if it were a ClassA object. This is the essence of polymorphism.
As for whether it would actually return the proper result, that is going to depend on your implementation of ClassA. Certain implementation wouldn't work as this code is expecting them to, but other implementations would.
Let's use a concrete example here. Let's say that this is the definition of ClassA:
public class ClassA
{
public int first;
public int second;
public bool Equals(ClassA other)
{
if (other == null) return false;
return first == other.first &&
second == other.second;
}
}
Now let's say we have these objects:
ClassA a = new ClassA() { first = 1, second = 2 };
ClassB b = new ClassB() { first = 1, second = 2, third = 3 };
Here a.Equals(b) will be true. It has both of the properties that are checked for, so it considers them equal. Do you want these objects to be considered equal?
Another implementation would be to do this:
public bool Equals(ClassA other)
{
if (other == null) return false;
if (other.GetType() != typeof(ClassA)) return false;
return first == other.first &&
second == other.second;
}
Here we're explicitly checking (granted, at runtime, not at compile time) to be sure that the other instance isn't of a more derived type. In this case a.Equals(b) would be false. You can implement whichever semantics you prefer.