Contents
- 1 Should square be inherited from rectangle?
- 2 Is square a subclass of rectangle?
- 3 What has opposite sides parallel?
- 4 What are the similarities and differences between a square and a rectangle?
- 5 How do you prove a square is a rectangle?
- 6 Why is a square inheriting from rectangle a problem?
- 7 Is the Setwidth SetHeight in a rectangle immutable?
- 8 What is the problem if all objects are immutable?
Should square be inherited from rectangle?
Rectangle must not inherit from Square . A square is a special form of a rectangle (where all sides have equal length). And the constraint on Square will ensure that both height and width must be equal.
Is square a subclass of rectangle?
Because Square is a subclass of Rectangle, we could pass an instance of Square as the border parameter. A Square is special because it has its height equal to its width. Functionally, Squares do not behave like Rectangles. A Rectangle should have independent methods to change its height or its width.
Why square is not a rectangle?
A rectangle can be tall and thin, short and fat or all the sides can have the same length. Thus every square is a rectangle because it is a quadrilateral with all four angles right angles. However not every rectangle is a square, to be a square its sides must have the same length.
What has opposite sides parallel?
A parallelogram is a quadrilateral in which both pairs of opposite sides are parallel .
What are the similarities and differences between a square and a rectangle?
The main difference between them is that a square has all equal sides, whereas, in a rectangle, the opposite sides are equal. In other words, a square is a rectangle in which the adjacent sides are equal and the interior angles are equal to 90°.
What are the similarities and differences of a square and a rectangle?
Difference & Similarity between Square & Rectangle
| Square | Rectangle |
|---|---|
| Diagonals of square are equal | Diagonals of rectangle are also equal |
| Diagonals of square bisect each other | Diagonal of rectangle also bisect each other |
| All sides of square are of equal length | Only opposite side of rectangle are of equal length |
How do you prove a square is a rectangle?
Definition: A square is a quadrilateral with all four angles right angles and all four sides of the same length. So a square is a special kind of rectangle, it is one where all the sides have the same length. Thus every square is a rectangle because it is a quadrilateral with all four angles right angles.
Why is a square inheriting from rectangle a problem?
From a mathematical perspective, a square is-a rectangle. If a mathematician modifies the square so it no longer adheres to the square contract, it changes into a rectangle. But in OO design, this is a problem. An object is what it is, and this includes behaviors as well as state.
Is the behaviour of a square a rectangle?
Although a square is a rectangle, the external behaviour of the two representations is incompatible, so inheritance is invalid. This problem applies to squares and rectangles, circles and ellipses and many other physical objects that you may wish to model.
Is the Setwidth SetHeight in a rectangle immutable?
Rectangles and squares (in mathematics) are immutable values, like numbers. setWidth and setHeight don’t belong on a Rectangle Class if you expect it to retain its mathematic properties. When you make the rectangle and square classes immutable the problem goes away and a square can be used everywhere a rectangle can be used.
What is the problem if all objects are immutable?
If all your objects are immutable, there is no problem. Every Square is also a Rectangle. All the properties of a Rectangle are also properties of a Square. The problem begins when you add the ability to modify the objects. Or really – when you start passing arguments to the object, not just reading property getters.