Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I have a little bit confusion of interfaces. I know definition of the interfaces.But I am not clear about when and where to will be used. Please any body can explain it.

And another doubt is, shall we used interface without implemented some method in to the class?. Because, that method is not need to all that classes either some classes. What can we do that situations?
Posted

Please see these past discussions:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

As to your last question, it is not quite correct. The notion of "using interface" is quite ambiguous. When you use the interface type as a type, none of the method is ever implemented. But at the point you complete the code of some struct or class implementing some interface, all interface methods are implemented in full, otherwise the code will not compile. You can also use (and should use) the interface as a compile-time type for some variable or member. But, during run-time, the actual run-time type will always be some struct or class. Actually, before you get to interfaces, you should understand very well the run-time and compile-time types, through inheritance, virtual method and method override, a heart of OOP.

If "that method is not need to all that classes", it may simply mean that your code design is just bad. Perhaps you need to review a good number of use cases for interfaces to understand it.

—SA
 
Share this answer
 
v3
Comments
Abhinav S 21-Jan-13 2:56am    
5 of course.
Sergey Alexandrovich Kryukov 21-Jan-13 2:58am    
Thank you, Abhinav.
—SA
__TR__ 21-Jan-13 3:01am    
5!
Sergey Alexandrovich Kryukov 21-Jan-13 3:03am    
Thank you very much,
—SA
CPallini 21-Jan-13 3:37am    
5.
An interface definition is a contract that classes (when implementing) must follow.
Interfaces are commonly used to vary the implementation even though classes adhere to the common contract.
This may help -When To Use Interfaces[^]

Any method defined in any interface must have an implementation in the derived class.

A couple of beginner links should further help you out.
Interfaces in C# (For Beginners)[^]
Abstract Class versus Interface[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jan-13 2:59am    
Useful links, a 5.
—SA
Abhinav S 21-Jan-13 3:35am    
Thank you SA.
__TR__ 21-Jan-13 3:01am    
5!
Abhinav S 21-Jan-13 3:35am    
Thank you.
CPallini 21-Jan-13 3:38am    
5.

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