Archived

This topic is now archived and is closed to further replies.

Whats the point of interfaces?? (C#)

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites