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.