L In SOLID - Liskov Substitution Principle

In SOLID, L stands for Liskov's Substitution Principle. LSP states that the object of the derived class should be able to replace an object of the base class without any issue in the application or changing the behavior of the base class.

This is the third principle in the SOLID. L stands for Liskov's Substitution Principle. This principle was introduced by “Barbara Liskov” in the conference with the topic “Data Abstraction” in 1987.
 
Read my previous articles on SOLID.

Liskov's Substitution Principle 

 
LSP states that the object of a derived class should be able to replace an object of the base class without any issue in the application or changing the behavior of the Base Class.
 
In this principle, inheritance is the concept which we need to implement. Thus, in simple words, all characteristics of a father should be in the son so that the son can take the place of father.
 
In the below example, I tried to explain the characteristics of Father, Son, and Daughter. Let’s try to understand the below code.
 
First, create a class “Father”.
  1. public abstract class Father  
  2. {  
  3.     public string IsHelpful()  
  4.     {  
  5.         return "Father is Helpful";  
  6.     }  
  7.     public string Attitude()  
  8.     {  
  9.         return "Father is with Positive Attitude";  
  10.     }  
  11.     public virtual string Adventurous()  
  12.     {  
  13.         return null;  
  14.     }  
  15.     public virtual string Dependable()  
  16.     {  
  17.         return null;  
  18.     }  
  19. }  
In the above class, there are 4 characteristics of the defined; however, Father has 2 characteristics and 2 are null. Let’s try to create derived classes which are Son and Daughter respectively, as below.
  1. public class Son : Father  
  2. {  
  3.     public override string Adventurous()  
  4.     {  
  5.         return "Son is very Adventurous";  
  6.     }  
  7. }  
  8.   
  9. public class Daughter : Father  
  10. {  
  11.     public override string Adventurous()  
  12.     {  
  13.         return "Daughter is Adventurous";  
  14.     }  
  15.     public override string Dependable()  
  16.     {  
  17.         return "Daughter is very Dependable";  
  18.     }  
  19. }  
In the Son class, we have implemented one characteristic and in the Daughter class, we have implemented two characteristics. Now, it’s time to see the output.
  1. static void Main(string[] args)  
  2. {  
  3.       
  4.     Father Sam = new Father();  
  5.     Console.WriteLine(Sam.IsHelpful());  
  6.     Console.WriteLine(Sam.Attitude());  
  7.     Console.WriteLine(Sam.Adventurous());  
  8.     Console.WriteLine(Sam.Dependable());  
  9.     Console.WriteLine();  
  10.   
  11.     //---------------------------------------------------  
  12.   
  13.     Father Dinesh = new Son();  
  14.     Console.WriteLine(Dinesh.IsHelpful());  
  15.     Console.WriteLine(Dinesh.Attitude());  
  16.     Console.WriteLine(Dinesh.Adventurous());  
  17.     Console.WriteLine(Dinesh.Dependable());  
  18.     Console.WriteLine();  
  19.   
  20.     //---------------------------------------------------  
  21.     Father Sheeti = new Daughter();  
  22.     Console.WriteLine(Sheeti.IsHelpful());  
  23.     Console.WriteLine(Sheeti.Attitude());  
  24.     Console.WriteLine(Sheeti.Adventurous());  
  25.     Console.WriteLine(Sheeti.Dependable());  
  26.     Console.ReadLine();  
  27.   
  28. }  
Output
 
Father is Helpful
Father is with Positive Attitude
 
Father is Helpful
Father is with Positive Attitude
Son is very Adventurous
 
Father is Helpful
Father is with Positive Attitude
Daughter is Adventurous
Daughter is very Dependable
 
So, in the first class (Father), we have implemented 2 characteristics, in the second class (Son) we have implemented 1 characteristic, and in the third class (Daughter) we have implemented 2 characteristics. Now, when I am creating an Object of Father class and try to print the class properties, I am getting the output with 2 characteristics that are “Helpful” and “Positive Attitude”.
 
We inherit Father class in Son and when I am creating Object of Son class and trying to print class properties, I am getting an output with 3 characteristics that are “Helpful”, “Positive Attitude” and “Adventures” even though we implemented only 1 characteristic because we have inherited the Father class in it.
 
We inherit Father class in Daughter class and when I am creating Object of Daughter class and try to print class properties I am getting output with 4 characteristics that are “Helpful”,” Positive Attitude”,” Adventurous”, and “Very Dependable” even though we implemented only 2 characteristics because we have inherited the Father class in it.
 
So now from the above example, all the characteristics from Father can be replaced by Son or Daughter.
 
With the help of the above example, you know about Liskov's Substitution Principle. It has well-defined rules for using subtypes in place of the base type. By following these rules, and others in SOLID, you will have better software that is more maintainable, easier to extend, and less fragile.