Square vs Rectangle
Why is a rectangle and a square a bad example of an inheritance?
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
According to the Liskov Substitution Principle, any class that is the child of a parent class should be usable in place of its parent without any unexpected behaviour.
Although a square is considered a rectangle, a square has different behaviors than a rectangle.
If you were to change the height of Rectangle, width would not be affected.
With square, on the other hand, you would have to change both length and width.
Since square does not have all the behaviors from the “parent” rectangle ,
we can conclude that rectangle….
…..” You are NOT the father!”.
Written on September 19, 2019