What is the use of interface in projects except the use of loosely coupling? Does it reduce memory usage while passing it as argument? Please list the advandages deeply.
-
Is this a homework question of some sort? (Also, interfaces in and of themselves have virtually no effect on memory or CPU performance.) – millimoose Feb 13 '12 at 13:15
-
You'll find plenty of explanations on the web, just google "programming interface advantages"... Also duplicate of http://stackoverflow.com/questions/1035632/what-are-some-advantages-to-using-an-interface-in-c – ken2k Feb 13 '12 at 13:17
-
2_List the advandages deeply_ is a bit overbearing according to your own concise question. – Tim Schmelter Feb 13 '12 at 13:17
2 Answers
I think you should have look to oop example and specially Design pattern
whihc help you to understand why to use interface..

- 175,020
- 35
- 237
- 263
Another important advantage to programming to interfaces is that it provides a means for polymorphism. If I have a collection of IShape and IShape provides a CalculateArea()
method, I can supply a new shape to the project by adding a new implementation of IShape. So, yes, this is providing looser coupling by virtue of adhering to the open/closed principle. But, it's also allowing IShape to be treated abstractly by clients without needing to know which specific IShape it is.
Polymorphism is fundamental to object oriented design, and an interface is a way to achieve that (the other being inheritance).
I'd also throw in that interface implementation tends to be expressive in terms of code intentions. If I have some declaration like:
public class Foo : IDisposable, IPersistMyselfToDisk, IRaiseUpdateEvents
I can tell a lot about the class and what it does at a glance -- more so than I could if I simply buried that functionality somewhere in the details of the class. Again, this goes back to decoupling to some degree, but it stands on its own as well.
I think you're going to find that decoupling is wrapped up with just about all advantages of using interfaces since providing classes that are cohesive and loosely coupled is just about as fundamental to OOP as having classes with state and behavior.

- 6,080
- 6
- 26
- 37