During our discussion on OOP (see my posts for the last few days) one of the key concepts was the base class. The base class was a place where we could put code that would in turn be used by the classes that descend from the base.
The use of the base class allows us to take advantage of polymorphism, the ability to treat a lot of specific child classes as if they were the same less specific, more generic type (the base). Notice a point I made a moment ago. The base class contains code, that will be inherited.
What if you have a situation where you want a common base, but you don’t have any code that will be reusable by it’s descendants? Interfaces step in to fill the need.
One classic example given in every book is the Shape. You never have “a shape”, instead you have rectangles, squares, circles, triangles and more.
An example from the real world might be the concept of lunch. Every day my coworkers and I go out for lunch. Lunch has a common method called Eat, yet the implementation varies depending on the type of food we pick. With Mexican we can eat chips and tacos with our hands. For Oriental we use chopsticks. For Italian, better use that fork. We have the common concept of Eat, but the implementation for each food type is so different we probably won’t be able to share any code.
Implementing an interface in C# is not difficult. Drawing back on our Employee example from the last few days, let’s say in your company you wanted to define a new type called “Person”. You will never have a “Person” object, instead you will always create an Employee, Contractor, Volunteer, and the like. To implement this, let’s create a Person interface.
First, right click on your solution and select Add, New Item, and select Interface from the long list. Set the scope to public.
using System;
using System.Collections.Generic;
using System.Text;
namespace OOPDemo
{
public interface IPerson
{
string FirstName { get; set;}
string Lastname { get; set;}
string FullName();
}
}
There’s several areas I need to draw your attention to. First, after public we used the keyword interface. This is the signal to the compiler that this is a declarations only, and has no code.
Next is the name, you see I put the letter I in front of Person. A long standing tradition is to name all of your interfaces with the letter I in front, and it’s a tradition I’d suggest you stick to.
Notice the FirstName and Lastname areas, this is how we declare there will be properties with a get and set. The FullName line is how we specify there will be a method with the name FullName. Note too there is no scope qualifier in front of any of the return types (in this case they are all string). Because this is an interface, all items must have a pubic scope.
Now we need to actually use the interface. Let’s modify our Employee class to implement the Iperson interface. All it takes is a modification to our class declaration.
class Employee : IPerson
Indicating the Employee class will now implement the Iperson interface carries certain connotations. It means we must implement all of the properties and methods the Iperson interface declares. Since we already have FirstName, Lastname, and FullName we are in good shape. However, if we had not we would have to create those properties or methods we were missing.
Now, via polymorphism we can treat the Iperson just as if it were a base class. Let’s modify the ShowName method from yesterday to use an interface instead of a base class.
private void ShowName(IPerson per)
{
MessageBox.Show(per.FullName(), “ShowName Method”);
}
This now makes the ShowName more functional for us. In the future, if we do implement a Volunteer or Contractor they too could be used in the ShowName method.
Using interfaces will allow you to further extend the power of polymorphism. In addition it gives you a way to create base classes in situations where you have no common code to put in the base.
Tomorrow: More on Interfaces.
This topic on Interface really helped to understand as wel as to implement in my code and i request to add all the oops topics in the same way so that we can clear the idea of all concepts in oops………………………….Regards Priya Fernandes
Hi,
This Topic clearly mentions when we need to use Interface giving very own examples.
I must say the way u explained this topic on Interface is very much digestable,and easy to remember.
I m waiting for more topics on oops like this one.
Regards,
Vidya Dufare
I am really thankful to u Sir.It was a really helpful to clear my doubts regardnig why to use Interfaces in C#?
Sir ,
I am waiting for more OOPS topics realted to C#.u plz send some more real time questions.