Interfaces vs. abstract classes

tush1r picture tush1r · Apr 14, 2009 · Viewed 158.8k times · Source

In C#, when should you use interfaces and when should you use abstract classes? What can be the deciding factor?

Answer

JaredPar picture JaredPar · Apr 14, 2009

The advantages of an abstract class are:

  • Ability to specify default implementations of methods
  • Added invariant checking to functions
  • Have slightly more control in how the "interface" methods are called
  • Ability to provide behavior related or unrelated to the interface for "free"

Interfaces are merely data passing contracts and do not have these features. However, they are typically more flexible as a type can only be derived from one class, but can implement any number of interfaces.