Tuesday, December 21, 2010

Interface in c#

Well what is actually an interface? I will start with an example from real life. We use interfaces or interface all the time. When my dad told me how to dial my first telephone number from our home phone many years ago, I was pretty pleased. When we visited my uncle in their house I could use their phone just as easy.

Imagine if someone had to teach me all over again how to use the phone. I do not need to know how the interface is implemented for a phone. I can use the buttons in the panel (the interface in this case) and know that the phone willsubscribe to the general phone interface.

Let’s see interface characteristics in the “code world”

  • An iterface defines only the properties and method signatures and not the actual implementation. I mean that in a class declaration we implement fields,properties,methods and events. In an interface we just define what properties/methods the class should have.
  • Requires that classes which implement an interface must ”honour” the property and method signatures. Basically are like contracts for classes. Classes which implement an interface must implement the methods and the properties of the interface
  • Many classes can implement a particular interface method, in their own way
  • An interface may inherit from multiple base interfaces
  • A class may implement-inherit from multiple interfaces
  • A C# class may only inherit from one class

Let’s see a simple example

1) Create a asp.net web application with C#

2) Add a new item to your project, a class file, called Human.cs.

3) Let’s define an Interface, called IHuman.

Just above this line of code

public class Human

type the following

interface IHuman
void Read();
void Write(object obj);
int Height { get; set; }
int Weight { get; set; }


here we define properties and methods that the class that subscribes to this interface must implement.

We prefix the name of the Interface with an “I”. Just a widely accepted convention…

4) Now let’s implement the class

Here we declare (sign a contract so to speak, that the Human class will Implement the IHuman Interface)

public class Human:IHuman

Right after the line above type

private int height;
private int weight;

public Human(string s)


// implement the Read method
public void Read()
“Implementing the Read Method for IHuman”);

// implement the Write method
public void Write(object o)
“Implementing the Write Method for IHuman”);
// implement the property Height
public int Height
get { return height; }
set { height = value; }

// implement the property Weight

public int Weight
get{ return weight; }
set{ weight = value; }

As i said before the class must “honour” its contract with the interface.

Before you go on try to comment this code out

public void Read()
“Implementing the Read Method for IHuman”);

Try to build your application. Well as you see from the error message below, someone is not honouring his contract….

Human’ does not implement interface member ‘IHuman.Read()’

5) From our Page_Load event of the defaul.aspx page we can create a new instance of the Human class

Human myperson = new Human(“fofo”);
myperson.Weight = 78;
myperson.Height = 88;

Please note that i could have a Boy class that could implement the IHuman interface.

public class Boy:IHuman

each class (boy,human) should implement the Read method of the IHuman interface. But each class was free to implement it in its own way.

We could define the IHuman interface above as an abstract class.And then had our class inheriting from our abstract class. But if changed slighlty the implementation of our abstract class then the referencing classes would fail and so our application would fail. If we work with Interfaces and we are commited to follow good practices like, never to alter the Interface implementation after they have been deployed, we will not run into such problems.

No comments:

Post a Comment