The Problem with Square Inheriting from Rectangle
credit @Google

The Problem with Square Inheriting from Rectangle

Liskov's Substitution Principle

Liskov's Substitution Principle (LSP) is one of the five SOLID principles of object-oriented design. It states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In other words, if S is a subclass of T, then objects of type T should be replaceable with objects of type S without affecting the desirable properties of the program.

No alt text provided for this image
Liskov Substitution

The Flaw in the Design

In the context of the Square class inheriting from Rectangle, the violation of LSP becomes apparent. A Rectangle class has two independent dimensions: width and height. However, in the case of a Square, both width and height are constrained to be the same, as it's a special case of a rectangle where all sides are equal.

The Issue

The problem arises when you attempt to set the width and height of a Square object independently. If you allow this in the Square class, it could lead to inconsistencies. For instance, if you set the width of a Square to a certain value and then set its height to a different value, you break the inherent constraint that a Square's sides must always be equal. This inconsistency contradicts the expected behavior of a Square.

Potential Solutions

  1. Use Composition: Instead of inheriting from Rectangle, you could use composition to build a Square class that contains a Rectangle object. This way, you can encapsulate the behavior you need for a square, while avoiding the problems associated with changing one side independently.

No alt text provided for this image


2. Separate Hierarchy: Create a separate hierarchy for Square and Rectangle, both derived from a common base class like Shape. This hierarchy would avoid the problems of contradicting constraints and ensure that each class adheres to its own behavior.

No alt text provided for this image


3. Immutable Properties: Make the properties of width and height in both Rectangle and Square classes immutable. When you need to change the size of a Square, you would create a new Square instance instead of modifying the existing one. This way, you maintain the invariant that a Square's sides are always equal.

No alt text provided for this image


Conclusion

Inheritance is a powerful tool in object-oriented programming, but it should be used carefully to avoid violating important principles like Liskov's Substitution Principle. In the case of Square inheriting from Rectangle, the inherent constraint of equal sides in a Square clashes with the independent width and height properties of a Rectangle. By considering alternatives like composition, separate hierarchies, or immutable properties, you can create a more robust and maintainable class hierarchy that respects the principles of object-oriented design.

No alt text provided for this image


Remember that good software design involves thoughtful consideration of the relationships between classes, their behaviors, and the principles that guide their interactions.


By: Chong Leang UENG

Student of Holberton France, AR/VR

To view or add a comment, sign in

More articles by Chong Leang UENG

Explore content categories