Can someone tell me if this class structure is bad?
class abstract Parent{
public Child Foo(){
return new Child();
}
}
class Child : Parent{}
I’ve heard that referring to a derived type from a base type is always bad and signs of bad design. Can someone tell me why this is bad or even if it is bad?
To elaborate on dkackman’s answer, your factory ideally would return objects of child types, but declared as the parent type.
The basic idea is that your calling code shouldn’t care which child class it gets back. This is one concept of the Liskov Substitution Principle.