Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
/// Interface demo
Interface Iinterface1
{
// Function prototype
public void ShowDetails();
}

// First class using the interface
Class MyClass1 : Iinterface1
{
public void ShowDetails()
{
// Function body comes here
Response.Write("I'm in MyClass");
}
}

// Second class using the interface
Class MyClass2 : Iinterface1
{
public void ShowDetails()
{
// Function body comes here
Response.Write("I'm in MyClass2");
Response.Write("So, what?");
}
}

Above example explained the interface. But above can be achieved by directly create method in MyClass2. I think it creates confusion. Every interviewer asking about Interface.

Then why we use interface?
Can any one give good example?
Posted
Updated 6-Nov-11 4:31am
v3

Basically, interfaces are a way to provide some kind of multiple inheritance in C#.

Because you can only derive a class from a single base class (or things start to get very confusing) it is difficult to provide additional features without it. A Base class describes what a class is - Interfaces describe what a class can do.

They provide a contract: "If you implement the required bits, you can be treated as one of us". For example, if your Students class is derived from People, and implements IEnumerable, then it has all the features of People, but it can also be used in a foreach loop.

Where classes implement the same interface, they call all be used in the same way, and through the same method set, despite being unrelated classes otherwise.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 6-Nov-11 9:55am    
I wouldn't call it multiple inhertance as there is no implementation in an interface. Also names only imply what is being done, whereas you correctly stated that interfaces supply a contract. Every class implementing an interface has to have the correct methods and properties with the matching signature, the semantic of such a method can differ totally from one implementation to the other though.
Sergey Alexandrovich Kryukov 6-Nov-11 10:58am    
It is "officially" the weak form of multiple inheritance. This is not just words but a very important aspect. Please see my solution where I add a point which is often overlooked.
--SA
Sergey Alexandrovich Kryukov 6-Nov-11 10:50am    
My 5 just for mentioning multiple inheritance.
This "some kind" is called weak form of multiple inheritance.
--SA
RaisKazi 6-Nov-11 11:57am    
Voted 5 for "Contract" point. But I will go with Manfred's view on "multiple inhertance".
thatraja 6-Nov-11 12:13pm    
5!
In addition to other answers, I want to explain one thing which is usually overlooked.

Can interface be implemented by value types? Yes, by structures.

This is a very important fact. It allows for much more flexible form of polymorphism (http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^]), where the polymorphous containers operate so abstract objects (represented by some interface(s)), so the container remains agnostic even to the knowledge of the nature of implementing type, are they classes or not.

Besides, look at the answer by Griff and my comment to it. The types representing be the interfaces can participate in more than one polymorphous relationship requiring different interface types due to weak form of multiple inheritance.

—SA
 
Share this answer
 
Comments
RaisKazi 6-Nov-11 11:51am    
Your above link provided me one more below link, which is good indeed. 5ed for that. http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

But I still believe Design-Principles mentioned in my Answer(return a class in C#) are valid.
Sergey Alexandrovich Kryukov 6-Nov-11 12:38pm    
Thank you, Rais.

Yes, they are valid; I only think it's not contributing to clarification of the topic in question because your post is triggered by wrong idea by OP. In other word, an attempt to "fix" the OOP abuse by OP is only confusing; the real solution would be to deny the idea in first place as I did instead of an attempt to fix it. So, this post could be confusing to Swami, not helping. I'm sure that if you started from scratch you would come to really good design based on your really good understanding of OOP, its application and philosophy.

Respect,
--SA
RaisKazi 6-Nov-11 12:51pm    
Thank you SA. Deleted my Answer with all respect.
thatraja 6-Nov-11 12:18pm    
Yep, 5!
Sergey Alexandrovich Kryukov 6-Nov-11 12:38pm    
Thank you, Raja.
--SA
As Manfred mentioned using interface based design concept provides loose coupling, component-based programming, easier maintainability, makes your code base more scalable and makes code reuse much more accessible because implementation is separated from the interface.

Interfaces add a plug and play like architecture into your applications. Interfaces help define a contract (agreement or blueprint, however you chose to define it), between your application and other objects. This indicates what sort of methods, properties and events are exposed by an object. Hence we can say that an interface is a contract that defines the signature of some piece of functionality.

hope it helps :)
 
Share this answer
 
Interfaces loosen the coupling[^] between classes. If one programs against Interfaces[^] instead of concrete classes the actual implementation will not have an effect on the class using it. Programming against interfaces only makes sure that the thing we're using in a certain place will expose the expected functionality.
Side note: The correct functionality is not ascertained, but the method signatures have to match.

Class coupling is a so called code metric that can be analyzed with code analyzers escpecially the static ones[^]. Visual Studio has on built in that will also measure class coupling[^].

You might also find this article usefull: The Factory Pattern[^] and this link explaining software metrics[^].

If you still have questions after ploughing through all the links feel free to leave a comment.

Cheers!

—MRB
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 6-Nov-11 10:59am    
Good point about coupling, a 5.
Please see also my solution (well, I already mentioned why). :-)
--SA
RaisKazi 6-Nov-11 11:54am    
Agree with the Answer. 5ed.
Uday P.Singh 6-Nov-11 12:15pm    
5+ for the loose coupling point :)
thatraja 6-Nov-11 12:15pm    
Nice bunch, 5!
BTW fix the broken link(code analyzers escpecially the static ones)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Nov-11 11:12am    
My 5 for a link to this short article.
Please also see my solutions, one of them points out the important aspect which is usually overlooked.
--SA
RaisKazi 6-Nov-11 11:59am    
Nice link. :) 5ed.
You can create an object Like this: MyClass1 obj=new MyClass1();

Then invoke Iinterface1's ShowDetails Method like this: (Iinterface1)obj.ShowDetails();
 
Share this answer
 
For an interesting simple example, please see my two past solutions on form collaboration and the interesting discussion about this technique:

How to pass the Value one form to another form in windows form[^],
How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
Implementing an interface allows a class to become more formal about the behavior it promises to provide also they form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900