HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Understanding interface with animal classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
understandingwithanimalinterfaceclasses

Problem

I made this small program to demonstrate to myself an example of interface. I wanted to confirm whether it is correct and if there is anything more I should know about interfaces other than the importance of implementing polymorphism and multiple inheritance.

//program.cs
class Program
{
    static void Main(string[] args)
    {
        Dog oDog = new Dog();
        Console.WriteLine(oDog.Cry());

        Cat oCat = new Cat();
        Console.WriteLine(oCat.Cry());

        Console.ReadKey();
    }

//IAnimal.cs
interface IAnimal
{
    string Cry();
}

//Dog.cs
class Dog : IAnimal
{
    public string Cry()
    {
        return "Woof!";
    }
}

//Cat.cs
class Cat : IAnimal
{
    public string Cry()
    {
        return "Meow!";
    }
}

Solution

Your example accesses the cat through a variable of type Cat and the dog through Dog. This would work, even if both classes did not implement a common interface. Inheritance is not involved.

In order to really demonstrate the usefulness of polymorphism I suggest the following code:

interface IAnimal
{
    string Name { get; set; }
    string Cry();
}

class Dog : IAnimal
{
    public string Name { get; set; }
    public string Cry()
    {
        return "Woof!";
    }
}

class Cat : IAnimal
{
    public string Name { get; set; }
    public string Cry()
    {
        return "Meow!";
    }
}


And here is a possible test:

static void Main(string[] args)
{
    var animals = new List();

    animals.Add(new Dog { Name = "Robby" });
    animals.Add(new Dog { Name = "Fify" });
    animals.Add(new Cat { Name = "Mimy" });

    PrintAnimals(animals);

    Console.ReadKey();
}

private static void PrintAnimals(IEnumerable animals)
{
    foreach (IAnimal animal in animals) {
        Console.WriteLine("Here is {0}: {1}", animal.Name, animal.Cry());
    }
}


It demonstrates that different types of animals can be treated the same way, but behave differently. This is what polymorphism is about. The word means many (poly) shapes or forms (morph). You can add different types of animals to a collection.

The PrintAnimals method has a parameter of type IEnumerable allowing you to pass it different types of collections (arrays, lists, linked lists, stacks and many more, as yet another example of polymorphism). It uses a unique Console.WriteLine statement for all types of animals, without even knowing of which type an animal really is. The only thing it knows is, that it implements IAnimal. You could even add new types of animals later, without having to change the PrintAnimals method and it would still work as expected.

Code Snippets

interface IAnimal
{
    string Name { get; set; }
    string Cry();
}

class Dog : IAnimal
{
    public string Name { get; set; }
    public string Cry()
    {
        return "Woof!";
    }
}

class Cat : IAnimal
{
    public string Name { get; set; }
    public string Cry()
    {
        return "Meow!";
    }
}
static void Main(string[] args)
{
    var animals = new List<IAnimal>();

    animals.Add(new Dog { Name = "Robby" });
    animals.Add(new Dog { Name = "Fify" });
    animals.Add(new Cat { Name = "Mimy" });

    PrintAnimals(animals);

    Console.ReadKey();
}

private static void PrintAnimals(IEnumerable<IAnimal> animals)
{
    foreach (IAnimal animal in animals) {
        Console.WriteLine("Here is {0}: {1}", animal.Name, animal.Cry());
    }
}

Context

StackExchange Code Review Q#23057, answer score: 13

Revisions (0)

No revisions yet.