

So if a method on a superclass is declared to return an integer, then it shouldnt be overwritten in subclass to return a string. The provable properties are basically the type signatures of members. For updates about new posts, sites I find useful and the occasional insights you can follow me on Twitter. As you see, the implementation of the TCEO class is stricter than its parent class. The Liskov Substitution Principle only says that subclasses should not violate provable properties of the supertype. You can get all source files of this example at. In this snippet, the Dove can both walk(from the Bird class) and fly (from FlyingBird class) and the Penguin can only walk (from the Bird class).

#Liskov substitution principle code
To correct this, the developer would be forced modify the code to accomodate the instance of a Penguin (this means violating the OCP principle or adhere to LSP principle as below Thus, the "fly" method is not applicable to all types of birds violating the LSP principle. In this inheritance, much as technically a penguin is a bird, penguins do not fly. Here, the Dove can fly because it is a Bird. Wikipedia definition says If S is a subtype of T, then the objects of type T may be replaced with objects of S without. The below is a Java code snippet showing an example that violates the LSP principle. Nguyên tc thay th Liskov - Liskov Substitution Principle Nguyên lý ca OCP da trên hai thành phn chính là tru tng (abstraction) và a hình (polymorphism). Liskov Substitution Principle (LSP) Basically, the principle states that if in an object-oriented program you substitute superclass object reference with any of its subclass objects, it should not break the program. The classic example of the inheritance technique causing problems is the circle-elipse problem (a.k.a the rectangle-square problem) which is a is a violation of the Liskov substitution principle.Ī good example here is that of a bird and a penguin I will call this dove-penguin problem.

If an override method does nothing or just throws an exception, then you're probably violating the LSP. This principle ensures that inheritance (one of the OOP principles) is used correctly. Thus, the objects of the subclasses should behave in the same way as the objects of the superclass. The Liskov Substitution Principle (LSP): " functions that use pointers to base classes must be able to use objects of derived classes without knowing it." In this article, we will explain the Liskov Substitution Principle which focuses on the behavior of a superclass and its subtypes.
#Liskov substitution principle software
There are key concepts in Object Oriented Programming(OOP) that enables you to write robust, maintainable and reusable software components - classes, modules, functions, e.t.c. So far we have explained the Single Responsibility Principle(SRP) and Open-Closed Principle (OCP) in our series of articles on the S.O.L.I.D principles.
