menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

The Flaw i...
source image

Medium

1M

read

297

img
dot

The Flaw in Square Inheriting from Rectangle

  • The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
  • In the case of the `Square` class inheriting from the `Rectangle` class, there are problems with violating the principle.
  • The issue arises when the width and height of a square can be set independently, breaking the invariant that a square must always have equal sides.
  • To address this, a redesign of the hierarchy is proposed, introducing a `Shape` base class and implementing specific behavior in each shape class.

Read Full Article

like

17 Likes

For uninterrupted reading, download the app