Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
//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!";
    }
}

So, I did the above small program to demonstrate myself an example of interface. I wanted to confirm whether it is correct and are there anything more I should know about interfaces other than it is important to implement Polymorphism and multiple inheritance. Any suggestion is appreciated. Thanks.

share|improve this question
    
If you want to know more about interfaces, Code Review is not the right site. –  svick Feb 23 '13 at 16:14
    
Why is that? I am just asking my example to be reviewed. –  Md.lbrahim Feb 23 '13 at 16:17
1  
But you asked for more than that. “are there anything more I should know about interfaces” That doesn't have anything to do with reviewing your code. –  svick Feb 23 '13 at 16:19
add comment

4 Answers

up vote 10 down vote accepted

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<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());
    }
}

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<IAnimal> allowing you to pass it different types of collections (arrays, lists, linked lists, stacks and many more). 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.

share|improve this answer
    
Could you please elaborate a bit how this demonstrates more of interface? –  Md.lbrahim Feb 23 '13 at 17:10
2  
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 the interface. Inheritance is not involved. My PrintAnimals method does not need to know the Cat and Dog classes. PrintAnimals could be defined in a different assembly where Cat and Dog are not accessible (if they are not public). Both, cats and dogs, are accessed through the same variable IAnimal animal. You could even add a new kind of animal later. PrintAnimals would still work without any changes and without recompilation. –  Olivier Jacot-Descombes Feb 23 '13 at 17:24
    
Thanks. Very helpful. –  Md.lbrahim Feb 23 '13 at 17:26
    
"Polymorphism is the ability of derived classes inheriting from the same base class to respond to the same method call in their own unique way" -Beginning C# OOP By Dan Clark. So, in my example, I inherited from IAnimal and had different implementation of Cry depending on the class (Dog,Cat). So, is not that all? Why is it important to have a PrintAnimal method to demonstrate Polymorphism? –  Md.lbrahim Feb 24 '13 at 4:24
    
Your example does not respond to the same method call. It responds to Dog.Cry and Cat.Cry. Those are are two different methods. My example always reponds to the same method call IAnimal.Cry. You wrote Dog oDog = new Dog();. If you changed it to IAnimal oDog = new Dog();, you would be responding to the same method call. –  Olivier Jacot-Descombes Feb 24 '13 at 18:46
add comment

Your code is quite simple and works, there is not much to review.

Few points:

  1. You shouldn't use variable names like oDog. If the o is meant to indicate that it's an object, then that information already contained in the type of the variable, so the prefix is useless. If it indicates something else, then it's very confusing.
  2. Your code is not a very good example, because it would work exactly the same even without the interface.
share|improve this answer
    
Yes, about anything is an object in C#, and thousands of types are available in the class library, making it virtually impossible to find an appropriate prefix for one of them. –  Olivier Jacot-Descombes Feb 23 '13 at 16:24
    
How would it work exactly the same without interface? Without interface an Animal class would have the Cry() method and it would not vary according to the class it was inherited by e.g.Dog and Cat. –  Md.lbrahim Feb 23 '13 at 16:43
    
@Md.lbrahim There is no Animal class in the code you posted. And the Main() method really doesn't use the interface in any way. –  svick Feb 23 '13 at 16:50
add comment

An interface is like a contract. In you case - all clases that inherits from IAnimal must have a Cry() method... (which is really mean :) )

In in you example you are not really using the interface. try writing it like this:

IAnimal animal1 = new Dog();
Console.WriteLine(animal1.Cry());

IAnimal animal2 = new Cat();
Console.WriteLine(animal2.Cry());

And see that it still works.

Now try adding a method to your Cat class. Perhaps an Eat() method.

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

    public string Eat()
    {
      return "Meow i'm so full - mice is my favorite!";
    } }

And then try calling that method with animal2 instance. You will notes you can't!

animal2.Eat(); //<-- compile time error

Because we are now using animal2 only as IAnimal. And all we know about an IAnimal is that it can Cry()

share|improve this answer
    
Thanks. By Cry I meant the sound they make. –  Md.lbrahim Feb 23 '13 at 16:50
add comment

A better exercise/code snippet to demonstrate the usefulness of an interface would be something like:

static void PrintAnimal(IAnimal animal)
{
    Console.WriteLine(animal.Cry());
}

static void Main(string[] args)
{
    IAnimal dog = new Dog();
    IAnimal cat = new Cat();
    PrintAnimal(dog);
    PrintAnimal(cat);

    Console.ReadKey();
}
share|improve this answer
    
Could u please explain a bit how this is better? Thanks. –  Md.lbrahim Feb 23 '13 at 17:13
    
The client expected the interface (IAnimal) only cares about that interface and not the specific implementation (Dog, Cat, etc). Therefore you could have the PrintAnimal method(maybe class at some point) handle any kind of animal you may happen to want in the future.Your example would work whether you had an interface or not. –  E-Man Feb 23 '13 at 17:24
    
Thanks.Understood. –  Md.lbrahim Feb 23 '13 at 17:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.