views:

672

answers:

6

Duplicate

Interface vs Base class


With C#, when to use Interfaces and when to use Abstract Classes, what can be the deciding factor.

+7  A: 

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 derived from one class but can implement any number of interfaces.

JaredPar
-1? At least add a reason
JaredPar
@JaredPar : I guess it's to discourage answering duplicate questions (btw, I'm not the one who downvoted you)
Brann
@Brann, I've unfortunately seen that behavior before. Seems like it would make more sense to use the down vote on the OP. It's their responsibility to search for dupes.
JaredPar
I'll +1, if memory serves this is what was in the framework design guidelines. Great examples in there too.
JoshBerke
+1  A: 

Duplicate:

http://stackoverflow.com/questions/56867/interface-vs-base-class

Rik
+1. Still valid, in my opinion.
Dave Van den Eynde
If you think this is a dup, why have you posted this as a reply? It is more common to vote "dup" and add a comment / edit the OP.
Marc Gravell
I'll do that next time. :)
Rik
@Rik The question you link to is a language agnostic question while this is C# specific, so while there is a lot of overlap, the answers are not exactly the same. For a good example, see Zifre's answer.
Robert Gowland
Does it work differently in any other single inheritance language with interfaces?
Rik
Well there sure is the difference between a normal base class and one marked as "abstract" in C#, a notion other single inheritance languages with interfaces might not have.
Dave Van den Eynde
+1  A: 

The real question is: whether to use interfaces or base classes. This has been covered before.

In C#, an abstract class (one marked with the keyword "abstract") is simply a class from which you cannot instantiate objects. This serves a different purpose than simply making the distinction between base classes and interfaces.

Dave Van den Eynde
+1  A: 

Abstract classes and interfaces are semantically different, although their usage can overlap.

An abstract class is generally used as a building basis for similar classes. Implementation that is common for the classes can be in the abstract class.

An interface is generally used to specify an ability for classes, where the classes doesn't have to be very similar.

Guffa
+1  A: 

Another thing to consider is that, since there is no multiple inheritance, if you want a class to be able to implement/inherit from your interface/abstract class, but inherit from another base class, use an interface.

Zifre
A: 

The best answer I have seen is in this excellent book by Brad Adams and Krzysztof Cwalina

http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321545613

JoshBerke