Skip to main content

Liskov Substitution Principle


Liskov Substitution Principle

We will be discussing the Liskov Substitution Principle also known as LSP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software.

This principle said that Subtypes must be substitutable for their base types. In other words, if we substitute a superclass object reference with an object of any of its subclasses, the program should not break.

Example

Here we design three classes for employees and one abstract class and Implement abstract class on these classes,

  1. PermamentEmployee.cs
  2. ContractEmployee.cs
  3. TemporaryEmployee.cs
class LSP {
    public LSP() {
        Run();
    }
    public void Run() {
        Console.ReadLine();
    }
    public abstract class Employee {
        protected string FullName {
            get;
            set;
        }
        protected abstract string CalculateBonus();
    }
    public class PermamentEmployee: Employee {
        PermamentEmployee() {
            FullName = "Ashish";
        }
        protected override string CalculateBonus() {
            return "25000.00";
        }
    }
    public class ContractEmployee: Employee {
        ContractEmployee() {
            FullName = "Nirmal";
        }
        protected override string CalculateBonus() {
            return "75000.00";
        }
    }
    public class TemporaryEmployee: Employee {
        TemporaryEmployee() {
            FullName = "Sheeba";
        }
        protected override string CalculateBonus() {
            throw new NotImplementedException(); // Not Applicable LSP
        }
    }
}
C#

The above implementation has one problem, temporary employees are not applicable for the bonus in this case. CalculateBonus () does not perform any logic but must you have to define the body of this abstract signature.

Again, Modify the code as per the LSP Principle.

 I must define one interface and implement every class which one is applicable for the bonus.

class LSP {
    public LSP() {
        Run();
    }
    public void Run() {
        Console.ReadLine();
    }
    interface IEmployee {
        public string CalculateBonus();
    }
    public abstract class Employee {
        protected string FullName {
            get;
            set;
        }
    }
    public class PermamentEmployee: Employee, IEmployee {
        PermamentEmployee() {
            FullName = "Ashish";
        }
        public string CalculateBonus() {
            return "25000.00";
        }
    }
    public class ContractEmployee: Employee, IEmployee {
        ContractEmployee() {
            FullName = "Nirmal";
        }
        public string CalculateBonus() {
            return "75000.00";
        }
    }
    public class TemporaryEmployee: Employee {
        TemporaryEmployee() {
            FullName = "Sheeba";
        }
    }
}
C#

Conclusion

We can see that implementing the LSP is not that complicated but just the opposite. Most of us probably already implemented this principle many times in our code without knowing its name because in the object-oriented world Polymorphism is quite a big thing.

Comments

Popular posts from this blog

How to find the count to convert a pyramid into a square using C#.net

Introduction   Let’s develop a simple C# console application to find the count of convert a pyramid into a square. Getting Started In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project. Now we will select the ASP.NET 5 Console Application to create the console application, click on the OK button. Include the following references in the application: using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 methods 1.PrintPyramid – To print the triangle in given input. 2.ConvertPyramidToSquare = Convert triangle into square. Please refer the code snippet below: static   void  Main( string [] args)   {   try    {   PrintPyramid();   ConvertPyram...

Open Closed Principle

  Open Closed Principle We will be discussing the Open-Closed Principle also known as OCP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that should be open for extension but closed for modification. We should write a code that does not require modification every time a customer changes its request. Example Let us define a class that contains salary calculations. Let us imagine that we have a task where we need to calculate the total cost of all the developer salaries in a single company. To get started, we are going to create the model class first, public class EmployeeReport { public int Id { get ; set ; } public string Name { get ; set ; } public string Level { get ; set ; } public int WorkingHours { get ; set ; } public double HourlyRate { get ; ...