Liskov substitution principle

This is a discussion on Liskov substitution principle within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am learning Liskov substitution principle, http://en.wikipedia.org/wiki/Liskov_...tion_principle But the description is not common senses. Now I understand this ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Liskov substitution principle

    Hello everyone,


    I am learning Liskov substitution principle,

    http://en.wikipedia.org/wiki/Liskov_...tion_principle

    But the description is not common senses. Now I understand this principle to be, if we have some methods in base class, then we also need to implement the methods in derived class. My understanding correct? Anything else missing?


    thanks in advance,
    George

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    I believe it has more to do with what in C++ is called Dynamic Binding. In that case, dynamic binding is an application of the Liskov Substituion Principle since dynamic binding allows you to use objects in a given hierarchy without caring about their specific types.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    I have answered this elsewhere.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mario,


    In your senses, Liskov Substituion Principle is the same as polymorphic? If not, what in your senses is the differences? Could you provide some pseudo code about what code follows LSP pattern and what code does not follow?

    Quote Originally Posted by Mario F. View Post
    I believe it has more to do with what in C++ is called Dynamic Binding. In that case, dynamic binding is an application of the Liskov Substituion Principle since dynamic binding allows you to use objects in a given hierarchy without caring about their specific types.

    regards,
    George

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Subtyping and the LSP is the theoretical base on which the C++ derivation mechanism builds. This includes polymorphism.

    However, C++ does not guarantee the LSP. You can still write hierarchies where it doesn't hold. It's the programmer's responsibility to avoid this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Can you show me a sample which is not LSP and a sample which is LSP please? Good to learn by sample.

    Quote Originally Posted by CornedBee View Post
    Subtyping and the LSP is the theoretical base on which the C++ derivation mechanism builds. This includes polymorphism.

    However, C++ does not guarantee the LSP. You can still write hierarchies where it doesn't hold. It's the programmer's responsibility to avoid this.

    regards,
    George

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Almost 100% of all samples of inheritance is showing valid LSP code - I don't think I've ever seen an example that doesn't.

    As for an example that breaks LSP:
    Code:
    class base
    {
    public:
       virtual int foo() { ... };
    };
    
    class derived : public base
    {
    public:
        virtual int foo(int) { ... };
    }
    In this example, calling foo on a base-class pointer will call a different function than derived::foo(), which means that you don't get the same behaviour. In this case, because foo takes a new parameter in the derived class. It is possibly not a GREAT example, so if someone has a better example, please feel free to post one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The classic "don't derive Square from Rectangle" counter-example is a case where the LSP is violated in inheritance.

    matsp's example is incorrect. You can still use a derived just as a base, provided you use a base reference to refer to it, thus removing the name lookup rules.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    1.

    Quote Originally Posted by CornedBee View Post
    The classic "don't derive Square from Rectangle" counter-example is a case where the LSP is violated in inheritance.
    I did some search. All I found from Google about "derive Square from Rectangle" are tutorials about how to use inheritance, seems legal and elegant. It is appreciated if you could show what do you mean "derive Square from Rectangle" counter-example? A link or some pseudo code?

    2.

    Quote Originally Posted by CornedBee View Post
    matsp's example is incorrect. You can still use a derived just as a base, provided you use a base reference to refer to it, thus removing the name lookup rules.
    You mean call base::foo from derived class? If I am wrong, please correct me. :-)


    regards,
    George

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Quote Originally Posted by George2 View Post
    I did some search. All I found from Google about "derive Square from Rectangle" are tutorials about how to use inheritance, seems legal and elegant.
    The Rectangle derived square serves as an example of how careful one should be when building hierarchies on the "IS A" principle. Mathematically a square is a rectangle. However, often a square isn't a rectangle in a OO hierarchy.

    The first clue you should get about a square not being a rectangle is the fact the square would only need to inherit one of setWeight() or setWidth() from rectangle. Inheriting both would not only be wasteful but also error prone since a class user would assume if he sets the width of the square, its weight will also be set, whereas this is not true of rectangles. There are obvious solutions to this, but with every one you find, you'll get more immersed in incorrect subtyping as other problems that are harder to solve start to emerge.

    Meanwhile, another example where you are breaking the Liskov principle is when your member functions are depending on types:

    Code:
    void CShape::DrawShape(const CShape& obj) {
        if (typeid(obj) == typeid(CSquare))
             // call CSquare draw function
        else if (typeid(obj) == typeid(CCircle))
             // call CCircle draw function
    }
    Last edited by Mario F.; 02-25-2008 at 05:33 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use the same principle..
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 07-01-2009, 08:45 PM
  2. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 05:29 AM
  3. Backward substitution in gaussian elimination
    By Meander14 in forum C Programming
    Replies: 0
    Last Post: 09-30-2007, 08:02 AM
  4. Substitution Cipher Program Problem...
    By Junior89 in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2005, 05:02 PM
  5. principle components analysis
    By mijter in forum C++ Programming
    Replies: 0
    Last Post: 04-18-2002, 08:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21