·
SOLID principles basically form the fundamental guidelines for building
👉 object-oriented ,
👉 loosely coupled ,
👉 robust, maintainable and
👉 easily understandable applications .
A class should be open for extension but closed for modification
which essentially means we do not want our existing code to be modified causing potential issues or bugs .
Other developers in case of some functionality change should be able to extend our class and override some methods.
For ex:
public class VehiclePrice { public double Value(Vehicle v) { if (v instanceof Car) { return v.getValue() * 0.5; if (v instanceof Truck) { return v.getValue() * 0.8; } } } }
If we want to add one more type to this ,say e-bike,
we would have to modify the above class with one more if statement which goes against the principle.
A better way is to let the sub classes override the Value method as per their type.