locked
Difference between interface and abstract class RRS feed

  • Question

  • User-1159802018 posted

    Hi everyone,

    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!

    Martijn

    Friday, November 23, 2012 9:06 AM

Answers

  • User-434868552 posted

    @ Martijn2010

    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>,
    	IEnumerable
    

    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

    4.3. Choosing Between Class and Interface

    In general, classes are the preferred construct for exposing abstractions.

    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 interface.

    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.

    g.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 23, 2012 9:23 AM

All replies

  • User-434868552 posted

    @ Martijn2010

    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>,
    	IEnumerable
    

    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

    4.3. Choosing Between Class and Interface

    In general, classes are the preferred construct for exposing abstractions.

    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 interface.

    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.

    g.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 23, 2012 9:23 AM
  • User-434868552 posted

    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:

    http://lmgtfy.com/?q=Difference+between+interface+and+abstract+class

    g.

    Friday, November 23, 2012 9:31 AM