Liskov Substitution Principle in C#

Liskov Substitution Principle in C# with a real-time example

In this article, I am going to discuss the Liskov Substitution Principle in C# with a real-time example. Please read our previous article before proceeding to this article where we discussed the Open-Closed Principle in C# with an example. The Letter L in SOLID stands for Liskov Substitution Principle which is also known as LSP. As part of this article, we are going to discuss the following pointers in detail.

  1. What is the Liskov Substitution Principle in C#?
  2. Example without using the Liskov Substitution Principle.
  3. Example using the Liskov Substitution Principle.

What is the Liskov Substitution Principle?

The Liskov Substitution Principle is a Substitutability principle in object-oriented programming Language. This principle state that, if S is a subtype of T, then objects of type T should be replaced with the objects of type S.

In other words, we can say that objects in an application should be replaceable with the instances of their subtypes without modifying the correctness of that application. 

For example, the father is a teacher whereas his son is a doctor. So here, in this case, the son can’t simply replace his father even though both belong to the same family.

Example without using the Liskov Substitution Principle in C#:

Let us first understand one example without using the Liskov Substitution Principle in C#. In the following example, first, we create the Apple class with the method GetColor. Then we create the Orange class which inherits the Apple class as well as override the GetColor method of the Apple class. The point is that an Orange cannot be replaced by an Apple, which results in printing the color of apple as Orange as shown in the below example.

namespace SOLID_PRINCIPLES.LSP
{
    class Program
    {
        static void Main(string[] args)
        {
            Apple apple = new Orange();
            Console.WriteLine(apple.GetColor());
        }
    }
    public class Apple
    {
        public virtual string GetColor()
        {
            return "Red";
        }
    }
    public class Orange : Apple
    {
        public override string GetColor()
        {
            return "Orange";
        }
    }
}
Example Using the Liskov Substitution Principle in C#

Let’s modify the previous example to follow the Liskov Substitution Principle. Here, first, we need to a generic base class such as Fruit for both Apple and Orange. Now you can replace the Fruit class object with its subtypes either Apple and Orage and it will behave correctly.

namespace SOLID_PRINCIPLES.LSP
{
    class Program
    {
        static void Main(string[] args)
        {
            Fruit fruit = new Orange();
            Console.WriteLine(fruit.GetColor());
            fruit = new Apple();
            Console.WriteLine(fruit.GetColor());
        }
    }
    public abstract class Fruit
    {
        public abstract string GetColor();
    }
    public class Apple : Fruit
    {
        public override string GetColor()
        {
            return "Red";
        }
    }
    public class Orange : Apple
    {
        public override string GetColor()
        {
            return "Orange";
        }
    }
}

Now, run the application and it should give the output as expected. Here we are following the Liskov Substitution Principle as we are now able to change the object with its subtype.

In the next article, I will discuss the Interface Segregation Principle in C# with a real-time example. Here, in this article, I try to explain the Liskov Substitution Principle. I hope you enjoy this article.

1 thought on “Liskov Substitution Principle in C#”

Leave a Reply

Your email address will not be published. Required fields are marked *