12,950,268 members (78,077 online)
Tip/Trick
alternative version

#### Stats

8.3K views
2 bookmarked
Posted 16 Apr 2013

# Liskov principle implementation

, 24 May 2013 CPOL
 Rate this:
Simple example of "Liskov principle"

## Introduction

Let's assume there is type S which get derived from type `T`, i.e., type `S` is derived type of parent type `T`. Type `S` contains all [public and protected] functionality of the type `T` and it contains more functionality of its own.

E.g. you can use all of you parent’s money and you can have you own money.

Then according to Liskov principle object of type `T` may substitute object of type S without altering any of the desirable properties of that program.

E.g. In the financial deal your money may replace by your parent’s money.

## Background

Let me start my story.

Man evolution starts and the first man learns to make fire then learns farming and then starts developing factories that start destroying nature by pollution but there are few men that refuse to learn the evolution of destroying nature, they just learn to fire and farming.

Now I want to make a list of all men and show only those who are bad, i.e., they learn pollution.

Let’s start implementing our above mentioned scenario.

We have two abstractions: first is Evolution including the good evolution: learn to fire and farming, and second abstraction including all the good evolution plus it has one bad evolution of learning to pollute.

Now consider we have thousands of men out of which a few thousand learn bad evolution as well as good evolution they are of derived type `IBadEvolution `and refuse to learn the bad evolution, they just learn the evolution they are of base type `IEvolution.`

Now we want to build a list of all men that learn Evolution of type `IEvolution`

Then we want to display the list of all men that learn evolution as well as bad evolution from the list of evolution, i.e., substitute `IBadEvolution` for `IEvolution`.

## Using the code

Let's build our base type Evolution abstraction:

```public interface IEvolution
{
string Name { get; set; }
void LearnToFire();
void LearnToFarming();
} ```

Let's build our derived type Bad Evolution as an abstraction, it contains all the evolutions as well as bad evolution.

```public interface IBadEvolution : IEvolution
{
void LearToPollution();
} ```

For simplicity we just consider one man that learns only good evolution, i.e., refuse to learn bad evolution.

```public class SomeGoodMan:IEvolution
{
public string Name { get; set; }
public SomeGoodMan()
{
Name = "I am 1st good man ";
}
public void LearnToFire()
{
Console.WriteLine(Name + "learn to fire.");
}
public void LearnToFarming()
{
Console.WriteLine(Name + "learn to farming.");
}
} ```

We consider two men that learn good evolution as well as bad evolution.

```/// <summary>
/// </summary>
{
public string Name { get; set; }
{
Name ="I am First bad man ";
}

public void LearToPollution()
{
Console.WriteLine(Name + "learn to polution.");
}
public void LearnToFire()
{
Console.WriteLine(Name + "learn to fire.");
}
public void LearnToFarming()
{
Console.WriteLine(Name + "learn to farming.");
}

}
/// <summary>
/// </summary>
{
public string Name { get; set; }
{
Name = "I am nth bad man ";
}

public void LearToPollution()
{
Console.WriteLine(Name + "learn to polution.");
}
public void LearnToFire()
{
Console.WriteLine(Name + "learn to fire.");
}
public void LearnToFarming()
{
Console.WriteLine(Name + "learn to farming.");
}
}```

Let's build the list of persons that learn evolution and by using substitution principle let's display only those that learn bad evolution.

```public class DisplayEvolution
{
static void Main(string[] args)
{
//Let me add all population of humans to the base
List<IEvolution> evolution = new List<IEvolution>();

//Show only the list of bad mans
//Lets substitube base type for the derived type for lsp
{
}
}
} ```

So we end up with our simple example of Liskov principle.

## Points of Interest

I learned form this exercise that we can avoid the violence of "Liskov Principle" by identifying abstraction in our project.

## Share

 Web Developer Lionbridge India
Amey K Bhatkar, a “Microsoft .Net” Web Developer.
I am programmer by will and profession.
I have completed my MCA in 2011 and join software industry.
Presently I am working with Lion Bridge Technologies in Mumbai - India

## You may also be interested in...

 Pro

 First Prev Next
 My vote of 2 Oshtri Deka12-Jun-13 0:34 Oshtri Deka 12-Jun-13 0:34
 My vote of 2 Klaus Luedenscheidt24-May-13 18:16 Klaus Luedenscheidt 24-May-13 18:16
 My vote of 2 Oron Mizrachi16-Apr-13 12:41 Oron Mizrachi 16-Apr-13 12:41
 You did not copy enough text from Wikipedia... Andreas Gieriet16-Apr-13 9:07 Andreas Gieriet 16-Apr-13 9:07
 Re: You did not copy enough text from Wikipedia... Amey K Bhatkar24-May-13 0:16 Amey K Bhatkar 24-May-13 0:16
 Where is the Liskov principle demonstration code?? AdolfoPerez16-Apr-13 8:02 AdolfoPerez 16-Apr-13 8:02
 Re: Where is the Liskov principle demonstration code?? Amey K Bhatkar24-May-13 0:17 Amey K Bhatkar 24-May-13 0:17
 Last Visit: 31-Dec-99 18:00     Last Update: 25-May-17 18:55 Refresh 1