locked
Abstract class and interface

    Question

  • what is the difference between abstract class and interface
    Friday, December 04, 2009 4:58 PM

Answers

  • 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. 
    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, December 04, 2009 5:22 PM
    Moderator

All replies

  • 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. 
    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, December 04, 2009 5:22 PM
    Moderator
  • Excellent answer from Reed. :)
    Friday, December 04, 2009 6:29 PM