Jump to content
  • Advertisement
Sign in to follow this  
ididntdoit

[.net] interface or class?

This topic is 3947 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!