locked
What's the difference between Abstract classes and Interfaces?

    Question

  • What's de advantage ov Interfaces and vice versa the advantage of the Abstract Classes?

    Thanks

    Friday, February 16, 2007 7:17 AM

Answers

  • A good way to distinguish between a case for the one or the other for me has always been the following:

    1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
    Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

    2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
    Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.

    As said by someone else's reply: the main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface.
    By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.
    Friday, February 16, 2007 8:01 AM
  • An abstract class is a class that can not be instantiated but that can contain code.
    An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.

    If you have logic that will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.

    You can implement multiple interfaces but only inherit from one class.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Friday, February 16, 2007 7:43 AM
  • Hi, Garegin

    An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.

    An interface can have neither of the two.

    Since it is a frequently asked question, you can see the following answers by others:

    http://en.csharp-online.net/Interfaces_and_Abstract_Classes

    http://www.codeproject.com/csharp/abstractsvsinterfaces.asp

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&SiteID=1

    If you have further questions, pls feel free to let us know. Thank you.

    Friday, February 16, 2007 8:37 AM
  • Good question... interfaces and abstract classes have so much in common it's hard to make a desision at times.

    Here's a quote from "The Complete Reference C# 2.0" by Herbert Schildt that I like:

    "When you can fully describe the concept in terms of "what it does" without needing to specify any of "how it does it", then you should use an interface.  If you need to include some implementation details, then you will need to represent your concept in an abstract class."

    Happy designing,

    Jim Tomasko

    Friday, February 16, 2007 8:39 AM

All replies

  • An abstract class is a class that can not be instantiated but that can contain code.
    An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.

    If you have logic that will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.

    You can implement multiple interfaces but only inherit from one class.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Friday, February 16, 2007 7:43 AM
  • A good way to distinguish between a case for the one or the other for me has always been the following:

    1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
    Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

    2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
    Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.

    As said by someone else's reply: the main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface.
    By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.
    Friday, February 16, 2007 8:01 AM
  • Hi, Garegin

    An abstract class can contain internal member variables, and can contain basic behaviour in the form of methods that have been coded.

    An interface can have neither of the two.

    Since it is a frequently asked question, you can see the following answers by others:

    http://en.csharp-online.net/Interfaces_and_Abstract_Classes

    http://www.codeproject.com/csharp/abstractsvsinterfaces.asp

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&SiteID=1

    If you have further questions, pls feel free to let us know. Thank you.

    Friday, February 16, 2007 8:37 AM
  • Good question... interfaces and abstract classes have so much in common it's hard to make a desision at times.

    Here's a quote from "The Complete Reference C# 2.0" by Herbert Schildt that I like:

    "When you can fully describe the concept in terms of "what it does" without needing to specify any of "how it does it", then you should use an interface.  If you need to include some implementation details, then you will need to represent your concept in an abstract class."

    Happy designing,

    Jim Tomasko

    Friday, February 16, 2007 8:39 AM
  • Thanks for such a good example!

    Hope we'll learn many things from each other.

    Friday, February 16, 2007 11:05 AM
  • Thank You.

    Friday, February 16, 2007 11:21 AM
  • Thanks for such good links.
    Friday, February 16, 2007 11:37 AM
  • There are three fundamental difference between interfaces and abstract base classes, two of which have already been described in this thread:

    1) Abstract base classes can include code (methods).
    2) Abstract base classes can include data (fields).
    3) You can multiply-inherit from interfaces, but you cannot multiply-inherit from abstract base classes.

    Monday, February 19, 2007 11:07 AM
  • I prefer not to use the term "inherit" when talking about interfaces - an interface is implemented Smile

    An interface describes in which ways a class can be "interfaced" (or interacted) with at runtime;  you can interact with the same object in multiple ways. When my class implements both IEnumerable and IDisposable, it does not inherit from multiple supers, but I can interact with this object in the ways described by the interfaces it implements (without knowledge of the actual type of the class!) Wink
    Tuesday, February 20, 2007 2:37 PM
  • Maybe so, but the terms tend to be used interchangably even within the official documentation:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcreftheinterfacetype.asp

    Tuesday, February 20, 2007 3:37 PM
  •  

    I think Ernst meant that a class that implements an interface doesn't "inherit" from the interface.  (Although interfaces can inherit from other interfaces, which I believe is what the link is talking about.)
    Tuesday, July 31, 2007 7:31 PM
  • Hey Sean, that's exactly what I meant Wink
    Even though these terms are 'officially' interchangable, I prefer to differentiate between them. Just personal opinion.
    Wednesday, August 01, 2007 8:48 AM
  • This one usually stays in a shadow. But in my opinion it's a killer.
    I like how this guy worded it:

     http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

    Adding functionality (Versioning)

    If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

    If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

    • Proposed as answer by felickz Wednesday, January 04, 2012 9:07 PM
    Tuesday, June 03, 2008 6:32 AM
  • I think we can certainly say that an abstract base class with no implementation or variable definitions are as good as interfaces. isn't it?

    -Phani

    Thursday, December 04, 2008 1:06 PM
  • so funny about the Cat  -- CatLover
    Monday, April 19, 2010 4:52 PM
  • Nice explanation. Even knowing, not everyone explain with the simplicity as you did.

    ~Mafruha

     


    MafruhaUnitedState
    Monday, August 16, 2010 6:23 PM
  • You can implement multiple interfaces but only inherit from one class.

    check more on this: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&SiteID=1

    http://www.codeproject.com/csharp/abstractsvsinterfaces.asp



    http://www.abdulazizfarooqi.wordpress.com BizTalk Developer UBL Transformation
    Tuesday, January 18, 2011 6:23 PM
  •      Features                                      Interfaces                                abstract class

    Multiple inheritance

    A class may inherit several interfaces.

    A class may inherit only one abstract class.

    Default implementation

    An interface cannot provide any code, just the signature.

    An abstract class can provide complete, default code and/or just the details that have to be overridden.

    Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties

    Core VS Peripheral

    Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

    An abstract class defines the core identity of a class and there it is used for objects of the same type.

    Homogeneity

    If various implementations only share method signatures then it is better to use Interfaces.

    If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

    Speed

    Requires more time to find the actual method in the corresponding classes.

    Fast

     

    Saturday, June 04, 2011 4:53 PM
  • A class may inherit several interfaces.

    A class can implement an interface. Only an interface can inherit from an interface. That's one of the mistakes you find even in good books, along with "all types inherit from Object".

    Requires more time to find the actual method in the corresponding classes.

    I don't think so. Why would it? Did you check it?
    Monday, June 06, 2011 1:11 PM
  • You know every time you prepare for an interview you look up for an answer for this..

    I must say this has to be the best answer ive ever heard.

    Thursday, September 15, 2011 8:48 AM
  • I have a doubt, its fine when we need some basic fuctionalities that is common to all classes we will use abstract classes..

    and if there is some contract functionality we need that will be implemanted differently by all the classes we'll use Interface..

    But one thing where i stuck most of the time in interviews is "If i am having only one fucntion with signature only then what should i implement Interface or abstract class?" as both will be having same functionality and in both the cases the class inherting them should have to provide implementation for the function.

    Friday, November 18, 2011 5:54 AM
  • I have a doubt, its fine when we need some basic fuctionalities that is common to all classes we will use abstract classes..

    and if there is some contract functionality we need that will be implemanted differently by all the classes we'll use Interface..

    But one thing where i stuck most of the time in interviews is "If i am having only one fucntion with signature only then what should i implement Interface or abstract class?" as both will be having same functionality and in both the cases the class inherting them should have to provide implementation for the function.

    There is no reason to use an Abstract class if you aren't going to provide implementations for any methods.  If all you're going to do is define a single abstract method, you should use an interface.
    Friday, November 18, 2011 5:00 PM
  • Thanks Servy..
    Wednesday, November 23, 2011 10:14 AM
  • Fantastic and to the point answer.
    Tuesday, December 20, 2011 7:47 AM
  • This is the short answer what i was looking for.
    Tuesday, December 20, 2011 7:49 AM
  • I have a doubt, its fine when we need some basic fuctionalities that is common to all classes we will use abstract classes..

    and if there is some contract functionality we need that will be implemanted differently by all the classes we'll use Interface..

    But one thing where i stuck most of the time in interviews is "If i am having only one fucntion with signature only then what should i implement Interface or abstract class?" as both will be having same functionality and in both the cases the class inherting them should have to provide implementation for the function.

    There is no reason to use an Abstract class if you aren't going to provide implementations for any methods.  If all you're going to do is define a single abstract method, you should use an interface.
            Yes, as servy told,it should be an interface,because may be the deriving class may need to inherit from another class which would not be possible,if the method is declared in an abstract class.

    Thanks, Satish Bommideni
    Tuesday, December 20, 2011 8:46 AM
  • If you want to wow your OO interviewer,  take Yakov72's answer into account and mention the SOLID principles and the concept that your code should be open for extension, but closed for modification.  So if you have an abstract base class you have the ability to extend your functionality with some method that contains a default implementation.  If you have to add a new method to an interface you then are forced to touch every implementation of said interface.  Enterprise level code should take this into serious consideration.
    Wednesday, January 04, 2012 9:12 PM
  • sorry, a dumb question here, why should we need to use even interfaces, first we need to define the methods and then only in the abstract class we can implement them and over the course of the project esp big projects things changes so much and if you have to ammend or append anything you have to follow the path again to update everything i.e. change the interface and then change your abstract class, right, so high maintenance. So why don't we simply use the normal C# classes, instead of this way.As I said before there may be something which I'm overlooking since I am a business/technical person and not from deep technical background.

    Cheers

    Friday, January 13, 2012 1:09 AM
  • I have to say there have been a lot of different answers which is fantastic. I love to see other developers' viewpoints and how they approach and look at things. I believe it enriches my knowledge. In terms of interfaces I have always understood them to be immutable meaning once they're defined they cannot be changed. Thus when new functionality is required and subsequently a new method must be added to the interface one should create a new interface with that method defined. Since we can implement multiple interfaces in our classes we would just specify the second interface for the classes that need it and write the required implementation.

    Also if there was a Dog class there would have to be a bark method and GoForRideInCar method. These methods were conceived by my border collie Roxanne. :)

    Sincerely,

    Denis J. Lanza

    Sr. .NET Developer

    IQPC

    Friday, January 13, 2012 2:58 PM
  • I think the best way interface and abstract class is explained is here by

    http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

    Regards

    Jaspal K.

    A guy with a bit of interest in programming

    Wednesday, June 13, 2012 8:23 AM
  • sorry, a dumb question here, why should we need to use even interfaces...

    Not a dumb question at all.

    Two main things (to me):

    1) A (real or abstract) class can implement multiple interfaces.  You can only inherit from one class in C#.  If you have complex subclass relationships, it can sometimes be difficult to get the effect you want with an abstract base class.  (Sometimes this is a clue that you're not modelling things well, but there are probably some contrived situations where the model is good but it would be very difficult to use a common abstract base class.)  Also, if totally unrelated objects (with no parent/child relationship at all) will be implementing the same interface, you have to use a true interface, since they cannot inherit from the same base class.

    2) An interface forces you to not know anything about what the actual implementing class will be, and define it only in terms of the method contracts.  Admittedly you can get a similar effect by defining an abstract class with only pure virtual functions and no fields.  In fact, this plus multiple inheritance is exactly how C++ implemented interfaces.

    Thursday, August 09, 2012 2:07 AM
  • 1. Interface only contains Skeleton methods (no implementation at all). An interface does not have member variables.

    2. Interface does not have access modifiers.

    3. However interfaces help us in achieving multiple inheritances i.e. a class cannot inherit multiple classes (also abstract class) but it can inherit multiple interfaces.

    4. Abstract class contains Skeleton methods as well as default implementation (common functionality).

    5. Abstract class contains member variables and access modifier (default is private).

    6. Use abstract class if you need to modify base class because once base class is modified, we don’t need to change sub classes. But in case of interface, once interface is created and used, we cannot change base class. In this case we may need to add new interface to accommodate required change.

    7. Use abstract class if you have to implement common functionality for closely related objects (default method with provide this).

    8. Use interface if you have to implement common functionality for uncommon objects or disparate objects (in sub class implementation will provide this).


    Saturday, November 03, 2012 9:31 PM