The discussion comes up here:
Changing visibility of method in inherited class
question is: is really "BTNode extends GraphNode" design a violation of Liskov's Substitution Princeple? As an "similar" example it was shown that case: Is deriving square from rectangle a violation of Liskov's Substitution Principle?
but I cannot really see why that is similar. I am very new to design, could someone explain me why (if) that's the case?
In Is deriving square from rectangle a violation of Liskov's Substitution Principle?, it basically says that
Square
cannot inherit fromRectangle
because there are things that you can do withRectangle
s but not withSquares
- setting its width to a different number from its height.You can't inherit
BTNode
fromGraphNode
because according to the original post you linked,GraphNode
has a method calledaddChild
.BTNode
on the other hand, can only have two children. Inheriting fromGraphNode
will also inherit theaddChild
method. This allows you to add multiple children toBTNode
, which aBTNode
cannot handle.Therefore,
BTNode
cannot inherit fromGraphNode
because there are things that you can do withGraphNode
s but not withBTNode
s - adding multiple children.For completeness, here is the Liskov's Substitution principle from Wikipedia
In simple terms,