Return to site

SOLID PRINCIPLES: Liskov Substitution Principle ๐Ÿช†๐Ÿ”ƒ

ยท java,coding,devops,solid

SOLID principles basically form the fundamental guidelines for buildingย 

๐Ÿ‘‰ object-oriented ,ย 

๐Ÿ‘‰ loosely coupled ,ย 

๐Ÿ‘‰ robust, maintainable andย 

๐Ÿ‘‰ easily understandable applications .

๐ƒ๐ž๐ซ๐ข๐ฏ๐ž๐ ๐œ๐ฅ๐š๐ฌ๐ฌ๐ž๐ฌ ๐ฆ๐ฎ๐ฌ๐ญ ๐›๐ž ๐ฌ๐ฎ๐›๐ฌ๐ญ๐ข๐ญ๐ฎ๐ญ๐š๐›๐ฅ๐ž ๐Ÿ”ƒ ๐Ÿ๐จ๐ซ ๐ญ๐ก๐ž ๐›๐š๐ฌ๐ž ๐œ๐ฅ๐š๐ฌ๐ฌ.

In simple words, If class A is extending class B, we should be able to replace B with A, without disrupting the behavior of our program.

As per OOPS concepts inheritance ๐Ÿช† is a good practice but Liskov principle mandates to use inheritance with a careful โš ๏ธ consideration.

We should only use inheritance if our super class is replaceable ๐Ÿ”ƒ with a sub class in all the instances.

For ex: Classic Square Rectangle problem.

 

๐€๐ฌ ๐ข๐ญ๐ฌ ๐œ๐ฅ๐ž๐š๐ซ ๐›๐š๐ฌ๐ž ๐œ๐ฅ๐š๐ฌ๐ฌ ๐‘๐ž๐œ๐ญ๐š๐ง๐ ๐ฅ๐ž ๐ข๐ฌ ๐œ๐ฅ๐ž๐š๐ซ๐ฅ๐ฒ ๐ง๐จ๐ญ ๐ซ๐ž๐ฉ๐ฅ๐š๐œ๐ž๐š๐›๐ฅ๐ž ๐›๐ฒ ๐ฌ๐ฎ๐› ๐œ๐ฅ๐š๐ฌ๐ฌ ๐’๐ช๐ฎ๐š๐ซ๐ž , ๐š๐ฌ ๐ฌ๐ช๐ฎ๐š๐ซ๐ž ๐ก๐š๐ฌ ๐š ๐œ๐จ๐ง๐ฌ๐ญ๐ซ๐š๐ข๐ง๐ญ ๐จ๐Ÿ ๐ž๐ช๐ฎ๐š๐ฅ ๐ก๐ž๐ข๐ ๐ก๐ญ ๐š๐ง๐ ๐ฐ๐ข๐๐ญ๐ก,

๐ฌ๐จ ๐ฌ๐ฎ๐›๐ฌ๐ญ๐ข๐ญ๐ฎ๐ญ๐ข๐ง๐  ๐‘๐ž๐œ๐ญ๐š๐ง๐ ๐ฅ๐ž ๐œ๐ฅ๐š๐ฌ๐ฌ ๐ฐ๐ข๐ญ๐ก ๐’๐ช๐ฎ๐š๐ซ๐ž ๐œ๐ฅ๐š๐ฌ๐ฌ ๐›๐ซ๐ž๐š๐ค๐ฌ ๐‹๐’๐.