Liskov Substitution Principle

From JholJhapata

The Liskov substitution principle states that if S is a subtype of T, then objects of type T may be replaced (or substituted) with objects of type S.

In other words a subclass should override the parent class methods in a way that does not break functionality.

Liskov Substitution Principle Example

In the below code we can notice that reference of Shapes can hold object of Square and Circle because it is inherited from Shapes.

   class Program
   {
       static void Main(string[] args)
       {
           Shapes _Shapes = new Square() { length=4, width=5};
           _Shapes.calculateArea();
           _Shapes = new Circle() { radius = 4 };
           _Shapes.calculateArea();
           Console.ReadLine();
       }
   }
   abstract class Shapes
   {
       public Shapes()
       {
           Console.WriteLine("Class : Shapes");
       }
       public abstract double calculateArea();
   }
   class Square : Shapes
   {
       public double length { get; set; }
       public double width { get; set; }
       public override double calculateArea()
       {
           return (length * width);
       }
   }
   class Circle : Shapes
   {
       public double radius { get; set; }
       public override double calculateArea()
       {
           return ((3.14) * radius * radius);
       }
   }