Click here to Skip to main content
13,247,448 members (73,721 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 27 May 2011

Liskovs Substitution Principle

, 27 May 2011
Rate this:
Please Sign up or sign in to vote.
This post is all about LSP.

I’m a big fan of patterns and principles. And I’m going to explain all SOLID principles in different blog posts. This post is all about LSP.

LSP says that you should always be able to use a base class or interface instead of the actual implementation and still get the expected result. If you can’t, you are breaking LSP.

An example:

public interface IDuck
   void Swim();
public class Duck : IDuck
   public void Swim()
      //do something to swim
public class ElectricDuck : IDuck
   public void Swim()
      if (!IsTurnedOn)

      //swim logic

And the calling code:

void MakeDuckSwim(IDuck duck)

As you can see, there are two examples of ducks. One regular duck and one electric duck. The electric duck can only swim if it’s turned on. This breaks the LSP since it must be turned on to be able to swim (the MakeDuckSwim method will not work if a duck is electric and not turned on).

You can of course solve it by doing something like this:

void MakeDuckSwim(IDuck duck)
    if (duck is ElectricDuck)

But that would break the Open/Closed principle (SOLID) and has to be implemented everywhere (and therefore still generate unstable code).

The proper solution would be to automatically turn on the duck in the Swim method and by doing so make the electric duck behave exactly as defined by the IDuck interface.

The solution with turning on the duck inside the Swim method can have side effects when working with the actual implementation (ElectricDuck). But that can be solved by using an explicit interface implementation. In my opinion, it’s more likely that you get problems by not turning it on in Swim since it’s expected that it will swim when using the IDuck interface.

What LSP is really saying is that all classes must follow the contracts that they have been given. Just as you may not update items in your website with GET if you are following the HTTP protocol.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

GeneralElectric Duck Pin
supercat931-May-11 6:03
membersupercat931-May-11 6:03 
GeneralRe: Electric Duck Pin
jgauffin31-May-11 10:34
memberjgauffin31-May-11 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 27 May 2011
Article Copyright 2011 by jgauffin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid