Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
4  
What can go wrong if you don't follow LSP? Worst-case scenario: you end up summoning Code-thulhu! ;) –  FrustratedWithFormsDesigner Oct 17 '12 at 16:00
    
As the author of that original question, I have to add that it was quite an academic question. Although violations can cause errors in code, I have never had a serious bug or maintenance issue that I can put down to a violation of LSP. –  Paul T Davies Oct 18 '12 at 13:29
1  
@Paul So yo never had a problem with your programs due to convoluted OO hierarchies (which you didn't design yourself, but maybe had to extend) where contracts were broken left and right by people who were uncertain about the purpose of the base class to begin with? I envy you! :) –  Andres F. Oct 24 '12 at 15:24
    
@PaulTDavies the severity of the consequence depend on whether the users (programmers who use the library) have detailed knowledge of the library's implementation (i.e. have access to and familiar with the library's code.) Eventually users will put dozens of conditional checks or build wrappers around the library to account for non-LSP (class-specific behavior). The worst case scenario would happen if the library is a closed-source commercial product. –  rwong Jul 19 '13 at 8:21
add comment

8 Answers

up vote 24 down vote accepted

I think it's stated very well in that question which is one of the reasons that was voted so highly.

Now when calling Close() on a Task, there is a chance the call will fail if it is a ProjectTask with the started status, when it wouldn't if it was a base Task.

Imagine if you will:

public void ProcessTaskAndClose(Task taskToProcess)
{
    taskToProcess.Execute();
    taskToProcess.DateProcessed = DateTime.Now;
    taskToProcess.Close();
}

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.

share|improve this answer
    
Does that mean that a child class can't have his own public methods that aren't declared in the parent class? –  Songo Oct 17 '12 at 19:32
    
@Songo: Not necessarily: it can, but those methods are "unreachable" from a base pointer (or reference or variable or whatever the language you use calls it) and you need some run-time type information to query what type the object has before you can call those functions. But this is a matter that is strongly related to languages syntax and semantics. –  Emilio Garavaglia Oct 17 '12 at 19:39
1  
No. This is for when a child class is referenced as if it were a type of the parent class, in which case members which aren't declared in the parent class are inaccessible. –  Chewy Gumball Oct 17 '12 at 19:40
    
So in other words, the more you violate the Liskov Substitution principle, the harder it is to maintain your code because you have to make changes in many places rather than just one place. –  Phil Oct 18 '12 at 14:01
1  
@Phil Yep; this is the definition of tight coupling: Changing one thing causes changes to other things. A loosely coupled class can have it's implementation changed without requiring you to change code outside of it. This is why contracts are good, they guide you in how not to require changes to your object's consumers: Meet the contract and the consumers will need no modification, thus loose coupling is achieved. When your consumers need to code to your implementation rather than your contract this is tight coupling, and required when violating LSP. –  Jimmy Hoffa Oct 18 '12 at 14:52
add comment

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.

LSP in wikipedia states

  • Preconditions cannot be strengthened in a subtype.
  • Postconditions cannot be weakened in a subtype.
  • Invariants of the supertype must be preserved in a subtype.

Should any of these not hold, the caller might get a result he does not expect.

share|improve this answer
    
+1: The only better than decent answer here. –  Thomas Eding Oct 17 '12 at 20:05
1  
Can you think of any concrete examples to demonstrate this? –  Mark Booth Oct 19 '12 at 14:27
1  
@MarkBooth The circle-ellipse / square-rectangle problem might be useful to demonstrate it; the wikipedia article is a good place to start: en.wikipedia.org/wiki/Circle-ellipse_problem –  Ed Hastings Nov 16 '12 at 2:45
add comment

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:

Ellipse.set_alpha_radius(d)
Ellipse.set_beta_radius(d)

Clearly, these must be re-defined for Circle, because a Circle has a uniform radius. You have two possibilities:

  1. After calling set_alpha_radius or set_beta_radius, both are set to the same amount.
  2. After calling set_alpha_radius or set_beta_radius, the object is no longer a Circle.

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:

some_function(Ellipse byref e)

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.

share|improve this answer
    
I think you can run into trouble if you use mutable objects. A circle is also an ellipse. But if you replace an ellipse which is also a circle with another ellipse (which is what you are doing by using a setter method) there is no guarantee that the new ellipse will also be a circle (circles are a proper subset of ellipses). –  Giorgio Oct 24 '12 at 15:23
    
In a purely functional world (with immutable objects), the method set_alpha_radius(d) would have return type ellipse (both in the ellipse and in the circle class). –  Giorgio Oct 24 '12 at 15:25
    
@Giorgio Yes, I should have mentioned that this problem only occurs with mutable objects. –  Kaz Dragon Oct 25 '12 at 8:48
add comment

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 .

share|improve this answer
2  
-1: all around bad answer –  Thomas Eding Oct 17 '12 at 20:12
2  
@Thomas I disagree. It's a good analogy. He talks about not breaking expectations, which is what the LSP is about. (though the part about case/switch is a bit weak, I agree) –  Andres F. Oct 24 '12 at 15:26
1  
And then Apple broke LSP by changing connectors. This answer lives on. –  Magus Feb 26 at 23:17
add comment

to give a real life example with java's UndoManager

it inherits from AbstractUndoableEdit whose contract specifies that it has 2 states (undone and redone) and can go between them with single calls to undo() and redo()

however UndoManager has more states and acts like an undo buffer (each call to undo undoes some but not all edits, weakening the postcondition)

this leads to the hypothetical situation where you add a UndoManager to a CompoundEdit before calling end() then calling undo on that CompoundEdit will lead it to call undo() on each edit once leaving your edits partially undone

I rolled my own UndoManager to avoid that (I probably should rename it to UndoBuffer though)

share|improve this answer
add comment

You can also look at it from a modelling point of view. When you say that an instance of class A is also an instance of class B you imply that "the observable behavior of an instance of class A can also be classified as observable behavior of an instance of class B" (This is only possible if class B is less specific than class A.)

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?

share|improve this answer
add comment

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.

I have Class A, which derives from Class B. Class A and Class B share a bunch of properties that Class A overrides with its own implementation. Setting or getting a Class A property has a different effect to setting or getting the exact same property from Class B.

public Class A
{
    public virtual string Name
    {
        get; set;
    }
}

Class B : A
{
    public override string Name
    {
        get
        {
            return TranslateName(base.Name);
        }
        set
        {
            base.Name = value;
            FunctionWithSideEffects();
        }
    }
}

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 Name is used as an index and a flow control variable in a number of places. The above classes are littered throughout the codebase in both their raw and derived form. Violating the Liskov substitution principle in this case means that I need to know the context of every single call to each of the functions that take the base class.

The code uses objects of both Class A and Class B, so I cannot simply make Class A abstract to force people to use Class B.

There are some very useful utility functions that operate on Class A and other very useful utility functions that operate on Class B. Ideally I would like to be able to use any utility function that can operate on Class A on Class B. Many of the functions that take a Class B could easily take a Class A if it was not for the violation of the LSP.

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 NameTranslated property, which will be the Class B version of the Name property and very, very carefully change every reference to the derived Name property to use my new NameTranslated property. However, getting even one of these references wrong the entire application could blow up.

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.

share|improve this answer
    
What would happen if within the derived class you shadowed the inherited property with a different kind of thing that had the same name [e.g. a nested class] and created new identifiers BaseName and TranslatedName to access both the class-A style Name and the class-B meaning? Then any attempt to access Name on a variable of type B would be rejected with a compiler error, so you could ensure that all references got converted to one of the other forms. –  supercat Dec 23 '13 at 21:19
    
I no longer work at that place. It would have been very awkward to fix. :-) –  Stephen Jan 7 at 1:01
add comment

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.

share|improve this answer
1  
This just opens up more questions rather than being an answer. –  Frank Oct 18 '12 at 5:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.