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.
public class Rectangle { private double height; private double width; public void setHeight(double h) { height = h; } public void setWidht(double w) { width = w; } //getters and calculateArea method } public class Square extends Rectangle { public void setHeight(double h) { super.setHeight(h); super.setWidth(w); } public void setWidth(double h) { super.setHeight(h); super.setWidth(w); } }
๐๐ฌ ๐ข๐ญ๐ฌ ๐๐ฅ๐๐๐ซ ๐๐๐ฌ๐ ๐๐ฅ๐๐ฌ๐ฌ ๐๐๐๐ญ๐๐ง๐ ๐ฅ๐ ๐ข๐ฌ ๐๐ฅ๐๐๐ซ๐ฅ๐ฒ ๐ง๐จ๐ญ ๐ซ๐๐ฉ๐ฅ๐๐๐๐๐๐ฅ๐ ๐๐ฒ ๐ฌ๐ฎ๐ ๐๐ฅ๐๐ฌ๐ฌ ๐๐ช๐ฎ๐๐ซ๐ , ๐๐ฌ ๐ฌ๐ช๐ฎ๐๐ซ๐ ๐ก๐๐ฌ ๐ ๐๐จ๐ง๐ฌ๐ญ๐ซ๐๐ข๐ง๐ญ ๐จ๐ ๐๐ช๐ฎ๐๐ฅ ๐ก๐๐ข๐ ๐ก๐ญ ๐๐ง๐ ๐ฐ๐ข๐๐ญ๐ก,
๐ฌ๐จ ๐ฌ๐ฎ๐๐ฌ๐ญ๐ข๐ญ๐ฎ๐ญ๐ข๐ง๐ ๐๐๐๐ญ๐๐ง๐ ๐ฅ๐ ๐๐ฅ๐๐ฌ๐ฌ ๐ฐ๐ข๐ญ๐ก ๐๐ช๐ฎ๐๐ซ๐ ๐๐ฅ๐๐ฌ๐ฌ ๐๐ซ๐๐๐ค๐ฌ ๐๐๐.