In what kind of inheritance relationship are rectangle and square

When a square is not a rectangle | Effective Software Design

in what kind of inheritance relationship are rectangle and square

A square can have an is-a relationship with rectangle. .. If a Square is a type of Rectangle than why cant a Square inherit from a Rectangle?. This reflects exactly the geometric relationship between them, since inheritance is commonly used to represent the is-a-kind-of relationship. public class Square extends Rectangle { public void setSide(double size) and in those cases you need to be extremely careful around inheritance. .. to derive types outside of the type signature and defined relationships.

Consider the following problem: So what I do is I set the length of the rectangle to 1. Unfortunately, someone has actually passed me a mixture of squares and rectangles, and when the length of the rectangle was changed, so was the width.

My unit tests pass because I wrote all my unit tests to use a collection of rectangles. I now have introduced a subtle bug into my application which can go unnoticed for months. Jim is happy and nobody is any wiser. Jim gets promoted for excellent work to a different division.

Alfred joins the company as a junior. Alfred sees that Squares and Rectangles are used everywhere in the code and realises that breaking the inheritance chain is impossible. He also does not have access to Accounting's source code.

in what kind of inheritance relationship are rectangle and square

So Alfred fixes the bug like this: The entire company goes into "priority 1 bugfix" mode to track down the source of the issue. They trace the problem to Alfred's fix. They know that they have to keep both Accounting and Advertising happy. So they fix the problem by identifying the user with the method call like so: This anecdote is based on real-world situations that face programmers daily.

They would worsen as further functionality was added. To begin, create the Rectangle class with properties for Height and Width and a method to calculate the shape's area: Currently Square is nothing more than a Rectangle with a new name. One way to update the class, to ensure that represented objects are indeed square, is to override the property setters.

Is a Square a Rectangle?

When one dimension is set, the other could be updated to match. The revised code is shown below: It is not possible to create a Square object with mismatched dimensions.


The most obvious violation of the LSP is a problem with invariants. Clients of the Rectangle class know that when the Height property is changed the Width property is invariant. With the introduction of the Square class, this rule has been broken. Consider the following code. Two Rectangle objects are created and their heights and widths are set.

Is a Square a Rectangle?

In the first case the height is set before the width and in the second, the width is set first. This does not matter as both properties are invariant when the other is changed.

The last line of code compares the two rectangles and determines that they have matching dimensions. However, if we substitute squares for rectangles the result of the comparison is false.

in what kind of inheritance relationship are rectangle and square

This is because changing one Square dimension affects the other and the order in which the width and height are specified has become important: As their functionality is extended further, the problems may be compounded. For example, the classes may be used to represent shapes in a computer-aided design CAD package. This software may require that a rectangle be stretched to fit within a given space whilst retaining its aspect ratio. Substituting a square might cause the stretching to produce a square that is too large or small.

If the package allowed a fixed value to be added to the height and width, squares may incorrectly have this value added twice.

object oriented - Should rectangle inherit from square? - Software Engineering Stack Exchange

The solution to the problem is not to include the inheritance relationship. An alternative may be to have a "Shape" base class or an "IShape" interface that is shared by rectangles and squares.

in what kind of inheritance relationship are rectangle and square

The base class would not include properties for Height and Width, instead allowing the dimensions of different shapes to be handled by their implementations. A Rectangle subclass would have Height and Width properties and a Square may have a single Size value.

Further classes, such as Pentagon, may be defined using multiple length and angle properties.

Another alternative to the Square and Rectangle problem may be to remove the Square class altogether.