I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what might go wrong if I as a developer do not think about the principle while writing object-oriented code.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
I think it's stated very well in that question which is one of the reasons that was voted so highly.
Imagine if you will:
In this method, occasionally the .Close() call will blow up, so now based on the concrete implementation of a derived type you have to change the way this method behaves from how this method would be written if Task had no subtypes that could be handed to this method.
Due to liskov substitution violations, the code that uses your type will have to have explicit knowledge of the internal workings of derived types to treat them differently. This tightly couples code and just generally makes the implementation harder to use consistently.
If you don't fulfill the contract that has been defined in the base class, things can silently fail when you get results that are off.
Should any of these not hold, the caller might get a result he does not expect.
Consider a classic case from the annals of interview questions: you have derived Circle from Ellipse. Why? Because a circle IS-AN ellipse, of course!
Except... ellipse has two functions:
Clearly, these must be re-defined for Circle, because a Circle has a uniform radius. You have two possibilities:
Most OO languages don't support the second, and for a good reason: it would be surprising to find that your Circle is no longer a Circle. So the first option is the best. But consider the following function:
Imagine that some_function calls e.set_alpha_radius. But because e was really a Circle, it surprisingly has its beta radius also set.
And herein lies the substitution principle: a subclass must be substitutable for a superclass. Otherwise surprising stuff happens.
In layman's words:
Your code will have an awful lot of CASE/switch clauses all over.
Every one of those CASE/switch clauses will need new cases added from time to time, meaning the code base is not as scalable and maintainable as it should be.
LSP allows code to work more like hardware:
You don't have to modify your iPod because you bought a new pair of external speakers, since both the old and the new external speakers respect the same interface, they are interchangeable without the iPod losing desired functionality .
to give a real life example with java's UndoManager
it inherits from
however UndoManager has more states and acts like an undo buffer (each call to
this leads to the hypothetical situation where you add a UndoManager to a CompoundEdit before calling
I rolled my own
Example: You are working with a UI framework, and you create your own custom UI-control by subclassing the
So what can go wrong with this? It is obvious that the rendering of the control will fail, since you don't return a list of controls as expected. Most likely the UI will crash. You are breaking the contract which subclasses of Control is expected to adhere to.
You can also look at it from a modelling point of view. When you say that an instance of class
So, violating the LSP means that there is some contradiction in your design: you are defining some categories for your objects and then you are not respecting them in your implementation, something must be wrong.
Like making a box with a tag: "This box contains only blue balls", and then throwing a red ball into it. What is the use of such a tag if it shows the wrong information?
I inherited a codebase recently that has some major Liskov violators in it. In important classes. This has caused me huge amounts of pain. Let me explain why.
Putting aside the fact that this is an utterly terrible way to do translation in .NET, there are a number of other issues with this code.
In this case
The code uses objects of both
There are some very useful utility functions that operate on
The worst thing about this is that this particular case is really hard to refactor as the entire application hinges on these two classes, operates on both classes all the time and would break in a hundred ways if I change this (which I am going to do anyway).
What I will have to do to fix this is create a
Given that the codebase does not have unit tests around it, this is pretty close to being the most dangerous scenario that a developer can face. If I don't change the violation I have to spend huge amounts of mental energy keeping track of what type of object is being operated on in each method and if I do fix the violation I could make the whole product explode at an inopportune time.
If you want to feel the problem of violating LSP, think what happens if you have only .dll/.jar of base class (no source code) and you have to build new derived class. You can never complete this task.