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”.
- public abstract class Father
- {
- public string IsHelpful()
- {
- return "Father is Helpful";
- }
- public string Attitude()
- {
- return "Father is with Positive Attitude";
- }
- public virtual string Adventurous()
- {
- return null;
- }
- public virtual string Dependable()
- {
- return null;
- }
- }
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.
- public class Son : Father
- {
- public override string Adventurous()
- {
- return "Son is very Adventurous";
- }
- }
-
- public class Daughter : Father
- {
- public override string Adventurous()
- {
- return "Daughter is Adventurous";
- }
- public override string Dependable()
- {
- return "Daughter is very Dependable";
- }
- }
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.
- static void Main(string[] args)
- {
-
- Father Sam = new Father();
- Console.WriteLine(Sam.IsHelpful());
- Console.WriteLine(Sam.Attitude());
- Console.WriteLine(Sam.Adventurous());
- Console.WriteLine(Sam.Dependable());
- Console.WriteLine();
-
-
-
- Father Dinesh = new Son();
- Console.WriteLine(Dinesh.IsHelpful());
- Console.WriteLine(Dinesh.Attitude());
- Console.WriteLine(Dinesh.Adventurous());
- Console.WriteLine(Dinesh.Dependable());
- Console.WriteLine();
-
-
- Father Sheeti = new Daughter();
- Console.WriteLine(Sheeti.IsHelpful());
- Console.WriteLine(Sheeti.Attitude());
- Console.WriteLine(Sheeti.Adventurous());
- Console.WriteLine(Sheeti.Dependable());
- Console.ReadLine();
-
- }
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.