System Design ( Day - 38 )

SOLID Principles | Liskov Substitution Principle Rules
Rules
1 . Signature Rule
a. Method Argument Rule
Whenever we are implementing the parent class methods we have to use the same type arguments or its parent class arguments. like the child classes argument should be equal or Broader( its Parent ).
b. Return Type Rule:
If the parent classes method is overrides then the return type should be same or its child class type (Narrow type). because the The parent can store the object of child or itself, but not its parent like Child can’t store the object of its parent.
c. Exception Rule
if the parent classes method is throwing some error, may be Runtime error, then the child has to throw the same error or its child error ( or runtime error’s child error ) not its parent error like ( not Runtime’s parent error).
2 . Property Rule
a. Class Invariant ( Rule)
The parent class have some rules like Account Balance >= 0 it should not be negative if any of the child class is not following this rule then this invariant rule will break.
b. History Constraint
The parent class will have some state or constraint like this should be followed like we have deposit( ) and Withdraw( ) methods are present in the parent class, but in the FixedDeposit class we should not allow him to withdraw( ) then this history constraint rule will break. so that’s why we have created multiple abstract classes to handle this in the previous articles.
3 . Method Rule
a. Pre-Condition
if we are setting some condition in the parent class for example num >= 0 or num <= 100 then the child class should follow this rule or it can narrow(Week) it down like num >= -10 or num <= 110 but it can elaborate(Strengthen it) it like num >= 10 or num <= 90 something like this, if this happens then the Pre-condition rule will break.
for example if the parent is giving the minimum password length should be >= 8 when the child is overriding then it can declare it as pass >= 6 this has been weaken not strengthen this is ok but pass >=10 this is not allowed.
b. Post-Condition
If we have some method in our Class Car, which is break( ) in the parent class if we apply breaks then it should slow down the speed of the vehicle, when it’s overriding the child class should follow the same breaks or it should strengthen like if we apply brakes then it should apply it with more threshold.
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
