Here's the code for applying Liskov Substitute Principle.
public abstract class Fruit
{
public abstract string GetColor();
}
public class Orange : Fruit
{
public override string GetColor()
{
return "Orange Color";
}
}
public class Apple : Fruit
{
public override string GetColor()
{
return "Red color";
}
}
class Program
{
static void Main(string[] args)
{
Fruit fruit = new Orange();
Console.WriteLine(fruit.GetColor());
fruit = new Apple();
Console.WriteLine(fruit.GetColor());
}
}
LSV states:
"Derived classes should be substitutable for their base classes (or interfaces)"
&
"Methods that use references to base classes (or interfaces) have to be able to use methods of the derived classes without knowing about it or knowing the details."