Liskov substitution principle - no overriding/virtual methods?

后端 未结 5 1358
旧巷少年郎
旧巷少年郎 2020-12-07 09:33

My understanding of the Liskov substitution principle is that some property of the base class that is true or some implemented behaviour of the base class, should be true fo

5条回答
  •  日久生厌
    2020-12-07 10:28

    There is one popular example which says if it swims like a duck, quack likes a duck but requires batteries, then it breaks Liskov Substitution Principle.

    Put it simply, you have a base Duck class which is being used by someone. Then you add hierarchy by introduction PlasticDuck with same overridden behaviors (like swimming, quacking etc.) as of a Duck but requires batteries to simulate those behaviors. This essentially means that you are introducing an extra pre-condition to the behavior of Sub Class to require batteries to do the same behavior that was earlier done by the Base Duck class without batteries. This might catch the consumer of your Duck class by surprise and might break the functionality built around the expected behavior of Base Duck class.

    Here is a good link - http://lassala.net/2010/11/04/a-good-example-of-liskov-substitution-principle/

提交回复
热议问题