# Squares and Rectangles

A square is not a rectangle?
rectangle => closed shape with straight edges and four right angles
square => rectangle with equal sides
What am I missing?

Iâm not sure what youâre replying to but this is a well known argument when discussing behavioral subtyping (a.k.a. the Liskov substitution principle). The problem is known as the âSquareâRectangle Problemâ or âCircleâellipse problemâ.

A short executive summary of the Liskov substitution principle (LSP) is: âAn object of a subtype can be used like an object of the supertype, without the code knowing that this is a subtype or how it behaves differently.â

In a language where a type `Rectangle` is mutable you can set the width and height independently of each other. If a type `Square` inherits from `Rectangle` things get complicated: What does it mean if the `width` or `height` gets modified? Should the other dimension also change or should the operation result in an error? Is either of those alternatives acceptable or do they violate the âdesirable propertiesâ of the `Rectangle` supertype?

My observation is that there are three groups of people in this discussion. The first argues that squares are rectangles so the inheritance hierarchy should reflect that. The second argues that from the viewpoint of the interface or the postconditions objects of the two types behave differently so they should be independent types. The third groups tries to resolve the contradiction by differentiating between the specification and the implementation, or introducing new concepts like âemerging propertiesâ.

You can find lots of discussions about this on the web, e.g. on StackOverflow or on softwareengineering.stackexchange.com.

2 Likes

Thanks for replying. I was making an observation as an ex Maths teacher, where in introducing Set logic examples such as âsquares are a subset of rectanglesâ are common.
If squares arenât subsets of rectangles in behavioural subtyping land then definitions need to be amended surely? Donât the rules of elementary Maths apply?
I agree it boils down to definitions.

âSubsetâ in the math realm doesnât necessarily mean âsubtypeâ in object inheritance.

I think thatâs my point. If one is going to use Math examples, then one needs to follow the Math rules, not invent new ones. Call them something else by all means.

Your issue is not with the subtype concept, but with the use of the terms square and rectangle. What have been called such above are in fact not at all the familiar geometric objects.

I think thatâs the point of that title You read the title, think, âBut a square is a rectangleâ then read how having a Square object inherit from a Rectangle object doesnât necessarily work very well. The title isnât actually what is being argued; the title is meant to catch your attention and make you think about inheritance.

While this discussion comes up I have a question to those OOP gurus. I didnât do a lot of OOP in the past years. My main language is Go for many years. But Iâm wondering if some should really care about that problem, using a object âsquareâ (it just should fullfil all math constraints to me and work great with inheritance).

Isnât it that this is exactly what the object should do: Encapsulate its logic? Letâs say most animals walk forward. And now there are those who do walk sideward. I donât care as long as I can say: You animal, please walk. Wrong? I mean, interfaces - like I use and know them - say what I can do with something. But not what will happen with a specific instance. Isnât that true for inheritance or composition, too?

This does an awful lot of work. (And I do not know what, exactly.)

The latter is part of the interface.

Example of interfaces that offer no guarantees about âwhat will happenâ at all are very few, and all are completely useless.

I canât agree with that. I worked on plugin systems or data aggregration stuff and others. Thatâs exactly the point, that you do not know whatâs going on. Thatâs why you have the contract of input and output. Letâs say image filters. You will have no idea what this filter is doing with your image. But you can be sure, youâll get an image back. Like the walking animals. They will give you coordinates where they are, but you have no idea how they did it.

Maybe thatâs really a problem of OOP with too much state in the objects. Or too much publy properties. I donât know.

Such is promised by the interface: you will get something back, and moreover it is an image.

An interface that would make truly no guarantees would have all methods be `void`, or have return type `â a. a` (i.e. a value that you do not know anything about), or something.

Needless to say, such an interface would also make no promisses about interactions between these methods. (Not that it matters â any effects of those methods would be unobservable anyway.)

Reading my quoted block again, Iâm sure I got it totally wrong on my way of translation. But I still think the âwhat will happenâ has not to be defined. But what will be the expected output.

Hi everyone, just created a new thread to keep the original thread What are your favorite books on programming? - #11 by DeepspaceFreelancer on topic.

Go to town on maths in this thread ;-)

Thank you @jonathanmiddleton , sorry for the discoure.

1 Like

Haha @sebastian0x62 I really enjoyed the threadâŚI never knew rectangles and squares could generate so much!