Sign in to follow this  

[.net] interface or class?

This topic is 3729 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

Interface cant define (only declare) variables, functions etc... Classes can. If youre looking for plug and play code, wouldnt classes be the way to go? What other uses are there for interfaces?

Share this post


Link to post
Share on other sites
You can only inherit from a single class, but you can implement any number of interfaces. Which is more appropriate depends highly on the context, but in general I find myself favoring interfaces over classes in general, especially when the type in question is intended as an extensibility point (because then it imposes less restrictions on the client code; if the extensibility point were a class, I've "used up" the one class that the user's implementation can derive from, which is sometimes undesirable).

I usually only elect to implement something as a class if it isn't an extensibility point (obviously), or if there's signifigant nontrivial functionality it must contain, especially if the behavior of that functionality is important and not just the mechanism by which it is called.

Share this post


Link to post
Share on other sites
jpetrie really summed it up well, but I would also like to add that even your "base" class can implement interfaces. I think the really nice thing that the XNA Framework has is its GameComponent and DrawableGameComponent classes implement specific interfaces. This allows you to just cut through the classes and implement the interfaces how you see fit.

Share this post


Link to post
Share on other sites

This topic is 3729 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this