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
Squarecannot inherit fromRectanglebecause there are things that you can do withRectangles but not withSquares- setting its width to a different number from its height.You can't inherit
BTNodefromGraphNodebecause according to the original post you linked,GraphNodehas a method calledaddChild.BTNodeon the other hand, can only have two children. Inheriting fromGraphNodewill also inherit theaddChildmethod. This allows you to add multiple children toBTNode, which aBTNodecannot handle.Therefore,
BTNodecannot inherit fromGraphNodebecause there are things that you can do withGraphNodes but not withBTNodes - adding multiple children.For completeness, here is the Liskov's Substitution principle from Wikipedia
In simple terms,