Whats the point of interfaces?? (C#)

Started by
5 comments, last by Elite19 20 years, 5 months ago
Hi, I''ve been learning about classes in c# and i think i understand most of class members except for interfaces. I dont understand why you would go out of the way to write an interface then have to retype all the method names ect in the class you are implementing the interface in. Is there a use for them that i''m not seeing here?
--------------------------------------------------------------------- There are 10 types of people in this world, those that understand binary and those who don't.
Advertisement
"Program to an interface, not an implementation."

If you aren''t familiar with that quote, then I''m afraid it may be a while before you understand what interfaces are really about.

But just to get you started, read this article;
http://www.codeproject.com/csharp/SnapIn.asp
Interfaces are for abstraction. With a class, you say something like "this is a hash table" or "this is a binary tree". With interfaces, you are able to say things like "this is an associative container". By defining standards (which interfaces help you do), you can have several implementations that externally appear the same but internally are very different. That means you can easily try many different things by changing only which implementation of the interface you use (you don''t have to change the code that actually uses it).
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Take a look at some of the interfaces in the BCL. Then look at the classes that implement them.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by Anonymous Poster
"Program to an interface, not an implementation."


Ever since I read the G.O.F book about design patterns I''ve lived by that quote.

------------------------------------------------------
50% of me is a huge nerd! How about you?
Interfaces are very similar to abstract classes. If you have an totally abstract class that has no method bodies then you can just stick it in an interface, but then all the methods have to be public.

personally i just stick to abstract classes, unless i want to simulate multiple inheritance, as i like to make my virtual/abstract methods private occasionally... things like that, and with interfaces i always feel like i'm... limited?

And yes, i have read the GoF book (about 100 times), but for most circumstances interfaces give you no real advantages over abstract classes (they are pretty much the same thing).

edit: "Program to an interface, not an implementation." <--- this is my mantra now since gof...

[edited by - jonnii on November 9, 2003 6:21:16 PM]
-jonnii=========jon@voodooextreme.comwww.voodooextreme.com
quote:Original post by jonnii
Interfaces are very similar to abstract classes. If you have an totally abstract class that has no method bodies then you can just stick it in an interface, but then all the methods have to be public.

personally i just stick to abstract classes, unless i want to simulate multiple inheritance, as i like to make my virtual/abstract methods private occasionally... things like that, and with interfaces i always feel like i'm... limited?

C#, like Java, has interfaces built in. Unlike C++ which will allow multiple inheritance with abstract classes to be used to the same effect. IIRC, C# doesn't allow mutliple inheritance so interfaces are both clearer (in expressing the original intent of the code) and more flexible (since they don't nail you down to a specific class hirarcy).

Its typically easy to go from an interface to inheritance if you find you've made the wrong choice as well, so starting with interfaces is generally a good rule of thumb.

[edited by - OrangyTang on November 9, 2003 6:26:33 PM]

This topic is closed to new replies.

Advertisement