-2

My sample program like below;

    public class Animal
    {
        public virtual string MakeSound()
        {
            return "General Sound";
        }
    }

    public class Dog : Animal
    {
        public override string MakeSound()
        {
            return "Bow..Bow..";
        }
    }
}

        static void Main(string[] args)
        {
            Animal obj1 = new Animal();
            Console.WriteLine("General Animal's sound id " + obj1.MakeSound());

            Dog obj2 = new Dog();
            Console.WriteLine("Dog Animal's sound id " + obj2.MakeSound());

            //Violate LSP
            Animal obj3 = new Dog();
            Console.WriteLine("Animal's sound id " + obj3.MakeSound());

            Console.ReadKey();
        }

Here as my understanding when we create Dog instance for Animal like obj3, we are violating LSP. Please confirm my understanding. If yes, please tell me how to achieve in this case to understand better. I think my coding is conceptionwise correct

Akhil
  • 1,918
  • 5
  • 30
  • 74

2 Answers2

0

You ar not violating Liskov subsitution rule.

Liskov subsitution rule in easy english -> if Base.Method leaves any invariants, then Derived.Method must mentain these invariants.

invariant is a state of an object which is not changed as result of a method execution.

for example:

public class MyInt{
   private int num1 = 5;

   public void print(){
      Console.write(num1);
   }    

   public void increment(){
     num1++;
   }

} 

here, the invariants of print are only num1, because it is not changed. the invariants of increment are non, because it changes all of MyInt members.

In you example:

the invariant that Animal.MakeSound has : non.
the invariant that Dog.MakeSound has : non.

the invariants of Animal.MakeSound are mentained in Dog.MakeSound, so the Liskov subsitution rule is not broken.

David Haim
  • 25,446
  • 3
  • 44
  • 78
  • ok so i am adding one more class called woodenDog which is toy dog made from woiod. So he can't make sound. Here if I create WoodenDog class inherit from Animal I am violatingLSP. correct? – Akhil Apr 27 '16 at 01:09
0

Your example doesn't really violate LSP. I think a better example of a violation would be something like

public class Bird
{     
    public virtual void Fly(int height);   
}

public class Penguin : Bird
{
    public virtual void Swim(int depth);
}

public static class BirdExtensions
{
    public static void Fly(this Bird bird)
    {
    }
}

If I pass an instance of Penguin to this method, I might have to throw a runtime exception or something because my Penguin can't fly :(

You see that we made an assumption about the base class (all birds can fly), but now we have an example of a child class that doesn't meet that assumption (penguins can't fly).

Furthermore, because a Penguin is-a Bird, it has the method for Fly(height=10), so I could technically do something like,

Bird b = new Penguin();
b.Fly(height=100);
b.Swim(depth=20);

Which violates the capabilities of the Penguin (he can't fly that high, he can only fly at 0, maybe...?).

Roly
  • 1,516
  • 1
  • 15
  • 26