What's the OO principle that states (in sum):
A base object should have no knowledge of its subtypes.
I thought it was Liskov Substitution but after reading that wikipedia article I don't believe I'm correct. Thanks in advance!
-
It might have been a reference to "Program to an interface, not to an implementation" (which means not to bypass an interface or to make assumptions about the implementation that the interface doesn't make explicit), or it might have been a reference to the "extension points" (specific places where the behavior of a base class is intended to be extended, i.e. don't modify base class behaviors that aren't meant to be modified.) Posted as comment since I don't have concrete reference.rwong– rwong12/08/2013 12:08:50Commented Dec 8, 2013 at 12:08
-
2When you say "children" do you mean other objects to which it holds references, or do you mean instances of subclasses of parent's class ? If the latter, then yes, a given instance of a class should not be aware (and, on some platforms, physically can't be aware) of whatever subclasses of it (and instances of those subclasses) exist.Shivan Dragon– Shivan Dragon12/08/2013 12:27:52Commented Dec 8, 2013 at 12:27
-
Thanks @ShivanDragon (+1) - yes I meant the latter, but was wondering what the name of the principle is, not the principle itself! Thanks again!herpylderp– herpylderp12/08/2013 12:42:22Commented Dec 8, 2013 at 12:42
2 Answers 2
This probably comes under dependency inversion.
The parent is a higher level abstraction than its child so it cannot be dependant on the child's implementation. It cannot know that a lower level abstraction exists or else it would by definition be dependant on it..
It is the Open Closed Principle.
Classes should be open to extension, but closed to modification.
If a class knows about its subtypes, that implies you can't just add one (extend the class) without modifying the base type (which should be prohibited).
-
1Dogmatism will kill us all one day. Subclasses are sometimes an implementation detail, and you may very well have a parent class that knows about its subtypes.user44761– user4476112/08/2013 21:49:48Commented Dec 8, 2013 at 21:49
-
1@tibo - Yes, there are very few absolutes in the world. If all of the types are internal, that becomes less offensive - but I can't think of a good example where this couldn't be done more cleanly another way.Telastyn– Telastyn12/08/2013 21:58:41Commented Dec 8, 2013 at 21:58
Explore related questions
See similar questions with these tags.