Re: OO Concept: Liskov Substitution Principle



Wayne wrote:
howa wrote:
Just read an article talking about the LSP in term of OO design:
http://www.objectmentor.com/resources/articles/lsp.pdf

The article said Rectangle class should not be a superclass of Square
class.

Okay, so how you would design the Rectangle & Square class?

The article did not cover that, so what are your opinion?
Howard

Clearly all Rectangles are not also Squares. Considering LSP,
Squares are not Rectangles either. Rectangles have two
*independent* values, height and width. With a Square the
height and width must be the same. What the article is
saying is that there is no IS-A relationship between
Squares and Rectangles.

If the article says that Rectangles must have different or independent values for height and width, then they are mistaken. It is permitted for a Rectangle to insist that the height and width be the same, in which case it is also a Square.

There is nothing wrong with having a Square class with both a setHeight() and a setWidth() method. Thus it remains compliant with Liskov.

--
Lew
.



Relevant Pages

  • Re: Enigma 1452 - Crossed lines
    ... calculated the number of rectangles which could be seen in this new ... large square grid. ... Suppose the new pieces had dimensions a x, ... Hence we derive an infinite sequence of pairs. ...
    (rec.puzzles)
  • Re: Enigma 1452 - Crossed lines
    ... calculated the number of rectangles which could be seen in this new ... large square grid. ... Suppose the new pieces had dimensions a x, ... Hence we derive an infinite sequence of pairs. ...
    (rec.puzzles)
  • Re: Distribution of the length of a random segment in an interval
    ... dimensional case (length of a random segment in a square) is much more ... (rectangles with sides parallel to the x and y axes), ... (or, more conveniently, the square of the distance) ... George Marsaglia ...
    (sci.math)
  • Re: delegation vs. inheritance
    ... will not be a subset of the set of propositions about rectangles. ... The process of abstraction was long ago made by ancient Greeks. ... But those are just additional constraints in the definition of a square that need to be abstracted. ...
    (comp.object)
  • Re: Partition a square into rectangles
    ... Prove/disprove that a 3000X3000 square can be partitioned into 5X9 ... rectangles ... cover an equal number of each distinct label. ... different number of some labels (for example, ...
    (rec.puzzles)