Basic purpose of an Interface

Hello everyone reading this post.
I know when anyone talks about OOP’s constructs , they will surely talk about Interface .
In general , it is thought of just like class without having any concrete method defined , and it also does not contain any field .
Yes it is , from definition point of view it is correct , and most of the developers and programmers are satisfied with this statement , but actually it is more than that .
The definition of interface that you get anywhere is actually the byproduct of what it is actually and what it facilitates .
So, let’s discuss what an interface is and why they come in OOPs world .
Interface provides contract between developers code or precompiled libraries code and runtime’s demand .
What is this runtime’s demand ? Don’t be afraid , just think about a scenario .
Suppose you want to write something on a paper with pen . You will hold a page and pen . Now , the time’s ( more technically runtime’s) demand is that when you move pen on the paper it should blow off ink . Its runtime demand , which is facilitated by pen’s manufacturer (developer). If manufacturing company didn’t care about ink (about Interface) , think what you would do when the pen didn’t have any ink in it or it does not blow off ink ! You will throw it off (It is very much similar to .NET exception) !

In programming world , these kind of things are documented in a programming construct known as an Interface .It provides a contract between runtime’s demand and code that it is going to execute .
As an example let’s consider IEnumerable interface .(One of my favorite interface) .
It provides facility to iterate over a collection . Aah , this is not 100% correct , did you noticed ?
Any interface is not going to provide any facility , how it can provide facility , when it’s every member is undefined !
It provides the contract that “If you need iteration behavior , then your actual implementation code (class) should implement me(interface) , because .NET runtime will later need those members (which are part of the interface) to call ” . That’s why , you can see every collection in .NET framework implements at least IEnumerable interface , and even if you want to create your own Collection , then your class should implement IEnumerable interface .

You can get example of IEnumerable interface at : http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx

Any comment regarding this blog is welcomed.

Advertisements

One thought on “Basic purpose of an Interface

  1. Yes, you are right. I have been stuck in the defination. And thinking of interface just as a blue print that restrict me in order for me to do certain thing I can use that blue print but i must implement it completly. For me, it is like instructions that have given to me by boss, that when i write the code to create a program for letterGeneration then I should implement that interface IGenerate. But when it comes to other usage I get kind of lost.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s