ferttrader.blogg.se

Liskov substitution principle
Liskov substitution principle






liskov substitution principle
  1. LISKOV SUBSTITUTION PRINCIPLE SOFTWARE
  2. LISKOV SUBSTITUTION PRINCIPLE CODE

Your ClientMethod() expects a Rectangle and asserts a value of the area. That is true for the language you are coding in and also for Mathematics but a Square might not always satisfy the behavior of a Rectangle. No matter how tempting it might sound or how well you intend to handle it, this small “reuse” will be totally WRONG!īy looking at the above scenario you might still argue that it looks pretty consistent. Will you inherit the Rectangle class inside the Square class? Now suppose you have a base class Rectangle and I tell you to create a new class Square and maximize reuse. It is after all a rectangle with equal sides, isn’t it? Remember in 5th grade when your maths teacher taught you the formula of squares for rectangles and squares? I always remembered square as the more “formula-friendly” rectangle. Let me give you a real-life example and it will be crystal clear to you. So the next time you create an object of the subclass by mistake, you should still be fine (given you implemented LSP).īut Mohit, what does this even mean? How can an object of another class behave the same way as another’s? You might ask.

liskov substitution principle

In other words, what we want is to have the objects of our subclasses behaving the same way as the objects of our superclass. Simply put, the Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. So hop on as this will be the only article you’ll ever need for understanding the infamous LSP! Liskov Substitution Principle (LSP) Well, guess what? The gods of Google have landed you on the right article because in this article we are going to talk all about LSP.

LISKOV SUBSTITUTION PRINCIPLE SOFTWARE

This is where the 5 most important software design principles come into play! Out of those, the most important and the most misunderstood principle is the Liskov Substitution Principle (LSP).

liskov substitution principle

LISKOV SUBSTITUTION PRINCIPLE CODE

Knowing the basics is one thing but writing good quality code is another. So your Object-Oriented Programming skills are at par and now you want to apply your knowledge to live projects.Īfter all, understanding the 4 basic pillars of Object-Oriented Programming and knowing a few cool things like constructor chaining and static methods should be enough to pull off the job right? Well, sorry to disappoint you but a S.O.L.I.D wall awaits! In this article, we are going to discuss what is Liskov Substitution Principle (LSP) with real world examples. Real-life Examples Which Break Liskov Substitution Principle (LSP).








Liskov substitution principle