System Design ( Day - 37 )

SOLID Principles | Liskov Substitution principle
L : Liskov Substitution principle
Definition: Sub Classes should be substitutable for their base classes.
That means we have a Base class A and child class B, which is inherited the function of the class A in B, this principle says like if we are sending the parent class’s object which is A, instead of that if we are sending the child classes object which is B, then there should be no problem with that, like every methods and variables are also available in the child class also then obviously there should be no problem should occur, we are not modifying the parent’s methods in the child we are just using it. and adding some more methods so
Let’s say we have a class A and Class B Class A has M1( ), M2( ) and M3 Methods init, in class B we have M4( ) and M5( ) methods in it the Class B is inheriting the Class A then the Class A is the Base class and Class B is the child class, whenever we are passing the class A object then we can only access the method M1, M2 and M3, but liskov principles says like instead of passing the object of class A we have to pass object of class B because obviously the methods of the class A will be definitely present in the class B also then the code should work perfectly.
but mostly everyone will break this rule let’s learn more about this below.
We’ll take the real time example, of a Bank
we have a abstract class Transaction in that class we have two methods which is Deposit( ), Withdraw( )
and we have 2 classes which is savings Account and CurrentAccount, the transaction class will be inherited in these two classes, and the methods will be inherited and Overridden, both the accounts has that access like both accounts can do the withdraw and deposit. but we need to add FixedDeposit account also, to the customers then we’ll inherit the Transaction class in to FixedDeposit class also, but in the fixedDeposit account, we can deposit the money but we can’t withdraw the money but when we are inheriting the Transaction class then the Deposit( ) and Withdraw( ) methods should be inherited but we don’t need the Withdraw( ) method here, we can throw an Exception but it will break the Liskov principle, like subClass can add more methods but can’t reduce the functionality.
Solution : We have to create a Abstract class which is NonWithdrawableAccount in this class we can add Deposit( ) method init and then we have to create another Abstract class which is WithdrawableAccount in that abstract class i need to add Withdraw( ) method and the NoWithdrawableAccount Class in Extended here then the NonWithdrawableAccount class is the base class then, WithdrawableAccount class is the child class, then we have 3 classes which is SavingsAccount, CurrentAccount and FixedDepositAccount,
SavingsAccount and CurrentAccount classes are inheriting the WithdrawableAccount class then they’ll get the Deposit( ) and Withdraw( ), but for FixedDepositAccount we inherit only the NonWithdrawableAccount then we’ll get only Deposit( ) not withdraw( ). This solves our problem and it will follow the Liskov principle.
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
