mardi 26 avril 2016

Understanding Liskov Substituion Principle

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

Aucun commentaire:

Enregistrer un commentaire