How do you explain OO to new programmers? [closed]

Seriously use Animals, it works great. And that’s what nailed the concept for me years ago. Just found this C# code. It seems good

    // Assembly: Common Classes
    // Namespace: CommonClasses

    public interface IAnimal
    {
        string Name
        { 
             get; 
        }
        string Talk();
    }

    // Assembly: Animals
    // Namespace: Animals

    public class AnimalBase
    {
        private string _name;
        AnimalBase(string name)
        {
           _name = name;
        }
        public string Name
        {
           get
           {
              return _name;
           }
        }
    }

    // Assembly: Animals
    // Namespace: Animals

    public class Cat : AnimalBase, IAnimal
    {
        public Cat(String name) :
            base(name)
        {
        }

        public string Talk() {
            return "Meowww!";
        }
    }

    // Assembly: Animals
    // Namespace: Animals

    public class Dog : AnimalBase, IAnimal
    {
        public Dog(string name) : 
            base(name)
        {
        }

        public string Talk() {
            return "Arf! Arf!";
        }
    }

    // Assembly: Program
    // Namespace: Program
    // References and Uses Assemblies: Common Classes, Animals

    public class TestAnimals
    {
        // prints the following:
        //
        // Missy: Meowww!
        // Mr. Bojangles: Meowww!
        // Lassie: Arf! Arf!
        //
        public static void Main(String[] args)
        {
            List<IAnimal> animals = new List<IAnimal>();
            animals.Add(new Cat("Missy"));
            animals.Add(new Cat("Mr. Bojangles"));
            animals.Add(new Dog("Lassie"));

            foreach(IAnimal animal in animals)
            {
                 Console.WriteLine(animal.Name + ": " + animal.Talk());
            }    
        }
    }

And once he’s got this nailed, you challenge him to define Bird (fly), and then Penguin (fly!?)

Leave a Comment