No, even though it's defined in the base class, the return type won't be different, it will still return an instance of A
which is not necessarily convertible to B. You could do something like this:
public class A{
public virtual A something() {
//...
}
}
public class B:A
{
public override A something() {
return new B();
}
}
B obj = new B();
obj = obj.something() as B;
Of course, the override does not really solve anything here. You could just return a B
in the base class and it would work the same way, but that's just awkward.
I think your issue is that you are not following the LISKOV substitution principle here: you should be able to replace every instance of A
with an instance that implements A
without affecting the client. Downcasting in general is a code smell. You should treat all instances as A
's and vary the behavior in your subclasses, not in the clients