Search code examples
rubyoopliskov-substitution-principle

What's wrong with the Square and Rectangle inheritance?


I've read some of article about the practice that making Square an inheritance class of Rectangle class is a bad practice, saying it violate the LSP (Liskov substitution principle). I still don't get it, I made a example code in Ruby:

class Rectangle
    attr_accessor :width, :height
    def initialize(width, height)
        @width = width
        @height = height
    end
end

class Square < Rectangle
    def initialize(length)
        super(length, length)
    end
    def width=(number)
        super(number)
        @height = number
    end

    def height=(number)
        super(number)
        @width = number
    end
end


s = Square.new(100)

s.width = 50

puts s.height

Could anyone tell me what's wrong with it?


Solution

  • I'm not always keen on Liskov since it seems to limit what you can do with inheritance based on behaviour rather than "essence". In my view, inheritance was always meant to be an "is a" relationship, not an "acts exactly like" one.

    Having said that, the wikipedia article goes into detail as to why this is considered bad by some, using your exact example:

    A typical example that violates LSP is a Square class that derives from a Rectangle class, assuming getter and setter methods exist for both width and height.

    The Square class always assumes that the width is equal with the height. If a Square object is used in a context where a Rectangle is expected, unexpected behavior may occur because the dimensions of a Square cannot (or rather should not) be modified independently.

    This problem cannot be easily fixed: if we can modify the setter methods in the Square class so that they preserve the Square invariant (i.e., keep the dimensions equal), then these methods will weaken (violate) the postconditions for the Rectangle setters, which state that dimensions can be modified independently.

    So, looking at your code alongside the equivalent Rectangle code:

    s = Square.new(100)            r = Rectangle.new(100,100)
    s.width = 50                   r.width = 50
    puts s.height                  puts r.height
    

    the output would be 50 on the left and 100 on the right.

    But, this is the important bit from the article, in my view:

    Violations of LSP, like this one, may or may not be a problem in practice, depending on the postconditions or invariants that are actually expected by the code that uses classes violating LSP.

    In other words, provided the code using the classes understands the behaviour, there is no issue.

    Bottom line, a square is a proper subset of a rectangle, for a loose-enough definition of rectangle :-)