Thursday, June 21, 2012

Interfaces

Source Code Interfaces Project

Interfaces are blueprints for classes to implement. With blueprints I mean the following: methods and properties can be declared in interfaces, but they must contain no code. Global variables can't be declared in an interface. When a class implements an interface, it must contain every method and property that exists in the interface. This must sound really useless, because why would you want a blueprint that classes can use when you can just type the methods and properties in the class without a blueprint.

Interfaces are used for the following:
  • Polymorphism (There will be a tutorial about this later on)
  • The developer can make "rules" in their software by forcing other developers (or himself/herself) to implement certain methods.
For this tutorial I will only show you the second part of the list shown above, because the concept of polymorphism is a lot to process when you don't know what interfaces are yet. If by the end of this tutorial you are not convinced that interfaces are handy, then you will probably be convinced when you see what you can do with polymorphism. Don't worry, I didn't think much of interfaces at first either.

Create a new console application called "Interfaces Tutorial".



Let's start by adding an interface. Click on "Project" in the menu (Next to File, Edit, View...) (1) and then click "Add New Item" (2).


Select "Interface" (1), give it the name "IAnimal" (2) and then click "Add" (3). The first thing you will notice is that instead of it saying "class IAnimal", it will say "interface IAnimal". The reason why I choose the name "IAnimal" is because every interface within the .NET Framework starts with the I from Interface and I try to keep my code consistent.


Add the following to your interface:
void makeSound();
This method has quite a few differences compared to a method in a class. First of all is that it has no access modifier, so no public, private, protected or internal. Second is that it has no code and no "{ }" signs. A method in a class doesn't end with a ";" sign either. This is what I meant with blueprint. It's just the data type for the return, the method's name and parameters. The code inside the method will be left entirely up to the developer that will implement the interface in a class.


Let's say that in a perfect world that every animal can make a sound. The reason why you would want to use an interface is because, for example, a cat makes a different sound than a dog. With an interface you give them the same method, but with different content.

An interface object can not be initialized like a class object. "IAnimal animal = new IAnimal();" will not work. 


Create a new class called "Cat".


In order for Cat to implement IAnimal, " : IAnimal" must be typed after "class Cat", so it will look like the following:
class Cat : IAnimal
It looks exactly the same when inheriting a class, but now an interface is used. You can implement and inherit at the same time by using the "," sign. If you have a class called "Creature" it will look like this: "class Cat : IAnimal, Creature". You can implement more than one interface in a class, unlike inheritance where a class can only inherit a maximum of one class.


If you try to run your application now, it will give you an error saying that Cat has not yet implemented the method "makeSound()" from the interface IAnimal.


Add the following code in order to fix the error:
public void makeSound()
{
Console.WriteLine("Meow");
Console.ReadLine();
}
Now the Cat class has implemented the "makeSound()" method and by doing that the error should be gone.

Next I would like to show you that you can implement multiple interfaces at once. Create a new interface called "ICarnivore".


Add the following to your ICarnivore interface:
void eatMeat();
Let's just say that every carnivore eats meat in a different way.


Switch back to your Cat class and add ", ICarnivore" to "class Cat : IAnimal", so it looks like the following:
class Cat : IAnimal, ICarnivore
You still have to make Cat implement the "eatMeat()" method or the compiler will generate an error, so add the following method to Cat:
public void eatMeat()
{
Console.WriteLine("I eat meat like a cat");
}
 Now you have implemented two interfaces in the same class. The only thing left to do is to actually use the Cat class.


Add the following code to your Program class' Main method:
Cat cat = new Cat();
cat.eatMeat();
cat.makeSound();
Press F5 to check your application and to test if everything works, then you're done!

Interfaces will play a bigger role the more experience you have, but don't go overboard. I know a lot of programmers that use an interface for every class when it isn't necessary. I hear a lot that interfaces are the new programming. That might be so, but I sometimes have the feeling that only students with no work experience say that. Then again, maybe I just don't understand this new programming.

Source Code Interfaces Project

>> References

No comments:

Post a Comment