This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# Liskov’s principle tends to be the most difficult to understand. The principle
# states that you should be able to replace any instances of a parent class
# with an instance of one of its children without creating any unexpected or
# incorrect behaviors.
classRectangle
definitialize(height,width)
@height=height
@width=width
end
defset_height(height)
@height=height
end
defset_width(width)
@width=width
end
defsquare
@width*@height
end
end
# Solution
# LSP says is if we know the interface of Rectangle, We need to be able to guess
# the interface of subtype class Square
# Square.new(3).square => 9
classSquare<Rectangle
definitialize(side)
super(side,side)
end
defset_height(height)
super(height)
@width=height
end
defset_width(width)
super(width)
@height=width
end
end
# Another common instance of a Liskov violation is raising an exception for an
# overridden method in a child class. It’s also not uncommon to see methods
# overridden with modified method signatures causing branching on type in
# classes that depend on objects of the parent’s type. All of these either
# lead to unstable code or unnecessary and ugly branching.