A certain failure of OOP is shown with a class Square inheriting from Rectangle, where logically Square is a specialization of Rectangle and should therefore inherit from it, but everything falls apart when you attempt to change a Square’s length or width.
Is there a specific term for describing what is going wrong with that case?
11
Wikipedia merely refers to it as the Circle-ellipse problem
The circle-ellipse problem in software development (sometimes known as the square-rectangle problem) illustrates a number of pitfalls which can arise when using subtype polymorphism in object modelling. The issues are most commonly encountered when using object-oriented programming.
This is the L in the acronym S.O.L.I.D. which is known as the Liskov substitution principle. This problem arises as a violation of that principle.
The problem concerns which subtyping or inheritance relationship should exist between classes which represent circles and ellipses (or, similarly, squares and rectangles). More generally, the problem illustrates the difficulties which can occur when a base class contains methods which mutate an object in a manner which might invalidate a (stronger) invariant found in a derived class, causing the Liskov substitution principle to be violated…
8
I would consider it a violation of the Liskov Substitution Principle – the Square
subclass specifically violates the invariant that length and width are independent.
At a more fundamental level than the Liskov Substitution Principle, this is a category error or category mistake
In the context of modeling behaviour a square simply is not a type of rectangle.
When you realize this the problem evaporates since the initial assumption (a square is a type of rectangle) is removed from play.
The issue with this answer is that since school it is drilled into anyone doing geometry that a square is a type of rectangle. But it is very important to understand that this is only true within a very specific context (the classification of geometric shapes based on the properties of their internal angles). In terms of behaviour a square is not a rectangle. To view one set of classification in the wrong context is a category mistake.