none
C#: what is difference between Abstract class and interface? How to decide to use which one?

    Question

  • I have been asked a question: how to decide to use Abstract class or Interface?

    We know that the Interface is special Abstract. All definitations are default public and without implemetion. Classed inherit from interface has to implement all definitions.

    Abstract class does not need to implement all the methods and it can not be instantiated.

    Classes inheritfrom abstract class may share common logic (properties or methods) with abstract class and also override some methods' behavior.

    How to clearily answer this question? Thx!


    JaneC




    Saturday, February 02, 2013 5:44 PM

Answers

  • Found the following link: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/d9ebdfca-18e7-4d73-89b4-5087d038f784

    Reed Copsey, Jr.  -  http://reedcopsey.com answered as:

    An abstract class contains implementation, where an interface is only a contract.

    From a theoretical standpoint, they should be used somewhat differently.  When you implement a class and derive from an abstract class, you're using inheritance, and you're basically saying that the new, concrete class IS-A more specialized type of the base class.

    When you implement an interface, though, you're really defining a standard contract for behavior that your class follows.  This really means that your class ACTS-AS the interface.


    From a practical standpoint, there are a few other differences.  Abstract classes give you the ability to consolidate your code - since you can implement properties and functions directly in the abstract class, you can reuse this code in all of the subclasses.  With an interface, each class has to implement the interface on its own.

    Interfaces, on the other hand, provide a way to have pseudo multiple inheritance. 

    Also, from a versioning standpoint - abstract classes are nicer.  You can add properties or methods to an abstract class without breaking an API.  An interface, on the other hand, can't be changed later, or it breaks the API and requires code changes in all classes that implement it.  This is partially why the design guidelines suggest to prefer abstract classes to interfaces, if all other choices are equal.

    In the following link: http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd Ernst Kuschke answered:

    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.


    JaneC

    Saturday, February 02, 2013 5:58 PM

All replies

  • Found the following link: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/d9ebdfca-18e7-4d73-89b4-5087d038f784

    Reed Copsey, Jr.  -  http://reedcopsey.com answered as:

    An abstract class contains implementation, where an interface is only a contract.

    From a theoretical standpoint, they should be used somewhat differently.  When you implement a class and derive from an abstract class, you're using inheritance, and you're basically saying that the new, concrete class IS-A more specialized type of the base class.

    When you implement an interface, though, you're really defining a standard contract for behavior that your class follows.  This really means that your class ACTS-AS the interface.


    From a practical standpoint, there are a few other differences.  Abstract classes give you the ability to consolidate your code - since you can implement properties and functions directly in the abstract class, you can reuse this code in all of the subclasses.  With an interface, each class has to implement the interface on its own.

    Interfaces, on the other hand, provide a way to have pseudo multiple inheritance. 

    Also, from a versioning standpoint - abstract classes are nicer.  You can add properties or methods to an abstract class without breaking an API.  An interface, on the other hand, can't be changed later, or it breaks the API and requires code changes in all classes that implement it.  This is partially why the design guidelines suggest to prefer abstract classes to interfaces, if all other choices are equal.

    In the following link: http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd Ernst Kuschke answered:

    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.


    JaneC

    Saturday, February 02, 2013 5:58 PM
  • good one
    Tuesday, March 22, 2016 9:33 AM