Recently I was asked by a colleague of mine about using interfaces, and the advantages associated with them. I went on a long speil extolling interfaces and I am sure bored the poor lad to tears. Now I intend on doing the same to yourselves by extolling the virtues of using interfaces here on my blog.
So why would we use an [tag]interface[/tag]? There are many reasons. Sure, there is some overhead involved in terms of design and programming, but those overheads are more than compensated for by the many advantages offered by interfaces.
An interface is similar to a base class, in that both provide a base structure and both can be used to achieve polymorphism. The difference is that a class can implement many interfaces, but can only [tag]inherit[/tag] one base class.
An interface cannot contain implementation, nor can it contain fields or constants.
Interfaces are widely used in the .NET framework. Familiarising ourselves with these as developers can help our understanding of the architecture of .NET. You will have come across them many times no doubt when developing .NET applications- eg. IComparable, IClonable, ISerializable, IEnumerable etc.
Take the [tag]IEnumerable[/tag] interface as an example. Any of the classes we develop can be used as a datasource (for a gridview, for example) or iterated through using a foreach loop by implementing the IEnumerable interface. We implement the IEnumerable interface in our classes as follows:
C#
public class ListOfClients : IEnumerable
VB.NET
Public Class ListOfClients
Implements IEnumerable
When we implement an interface in our class, the compiler is made aware of members of that interface, which the implementing class must include. All types (classes, structures etc) that implement an interface must implement every declared method in that interface. The compiler ensures developers handle this. Visual studio.net will offer to create the stub of an interface once it detects that you are implementing one.
An interface only contains abstract members (Events, Methods, Properties, Indexers), and all of these members are public. Also, as you may expect, an interface doesn’t contain code. It contains only declarations for its members. Any code is written in the class which implements the interface. An interface can’t contain constants, data fields, constructors, destructors and static members.
To declare an interface you use the Interface keyword, like this:
C#
public interface MyInterface
{
int ID {
get;
set;
}
string GetMyName();
}
VB.NET
Public Interface MyInterface
Property ID() As Integer
Function GetMyName() As String
End Interface
As you can see, interfaces are easy to create. The code above creates an interface called MyInterface. It declares one method – GetMyName, and one property – ID. The class which implements MyInterface will have to implement these routines. Here is an example:
C#
public class MyClass : MyInterface
{
protected int _id;
public int MyInterface.ID {
get { return _id; }
set { _id = value; }
}
public string MyInterface.GetMyName()
{
return “MyClass”;
}
}
VB.NET
Public Class MyClass
Implements MyInterface
Protected _id As Integer
Public Property ID() As Integer Implements MyInterface.ID
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Function GetMyName() As String Implements MyInterface.GetMyName
Return “MyClass”
End Function
End Class
By using interfaces we make it easier to implement functionality on a variety of platforms (mobile, web, touch screen, Usercontrol etc). Interfaces help define what our UI is doing, helping us separate the UI from business logic. Classes behave more predictably when we make use of interfaces, and this predictability is enforced by the compiler.
There is no point in me going into much more depth with regard to the usefullness of interfaces when there are fine articles available to those who care to read further, such as this one: http://aspnet.4guysfromrolla.com/articles/110304-1.aspx
The purpose of this blog is to give a quick overview of interfaces, and to highlight their part in making applications more scalable and ensuring their quality and predictability.