Last post Nov 23, 2012 09:31 AM by gerrylowry
Nov 23, 2012 09:06 AM|Martijn2010|LINK
Can anyone explain me the difference between an interface and abstract class? It's clear for me that all defined methods in an interface must be implement by a class. By an abstract class it doesn't.
At what moment do I use an interface or abstract class? Can anyone give me some practical examples so I do understand the difference. Also I would like to know in what situations
I can use it. Hope anyone can help me.
Thanks a lot!
Nov 23, 2012 09:23 AM|gerryLowry|LINK
Short answer: you may want to use an interface if you're going to need a form of multiple inheritance.
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx "List<T> Class"
List<T> uses several interfaces:
public class List<T> : IList<T>, ICollection<T>,
IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>,
Simply put, if you want to enforce a contract, then you may want an interface. However:
"Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries", Second Edition, Krzysztof Cwalina; Brad Abrams; Addison-Wesley Professional, October
22, 2008; Print ISBN-10: 0-321-54561-3, Print ISBN-13: 978-0-321-54561-9, Web ISBN-10: 0-321-54567-2, Web ISBN-13: 978-0-321-54567-1, Pages in Print Edition: 480
classes are the preferred construct for exposing
The main drawback of
interfaces is that they are much less flexible than
classes when it comes to allowing for evolution of APIs. After you ship an
interface, the set of its members is fixed forever. Any additions to the
interface would break existing types that implement the
A class offers much more flexibility. You can add members to
classes that have already shipped. As long as the method is not
abstract (i.e., as long as you provide a default implementation of the method), any existing derived
classes continue to function unchanged.
note: while the book is mainly applicable to designing API's, much of its information is also applicable generically.
Nov 23, 2012 09:31 AM|gerryLowry|LINK
@ Martijn2010 TIMTOWTDI =. there is more than one way to do it
For questions like "Difference between interface and abstract class", Google and/or your favourite search engine(s) is very helpful: