137

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

Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
tush1r
  • 18,129
  • 14
  • 32
  • 34

4 Answers4

148

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.

Stefanos Kargas
  • 8,783
  • 21
  • 69
  • 90
JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • @JaredPar : I guess it's to discourage answering duplicate questions (btw, I'm not the one who downvoted you) – Brann Apr 14 '09 at 13:34
  • 17
    @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 Apr 14 '09 at 13:37
  • 3
    I'm surprised this answer has so many upvotes and is the accepted answer. It seems to show the answerer's bias ("advantages of abstract class" followed by four bullets of advantages followed by "interfaces are merely" followed by a single "however" in favor of interfaces) toward abstract classes much more than really describe the differences between abstract classes and interfaces. I am not meaning to attack the answerer but certainly this question should be stated in a more objective way to do the best justice to the question. – Matthew Sep 27 '14 at 00:36
  • 4
    @Matt the answer provides all the relevant facts for both abstract classes and interfaces, so why wouldn't it be objective? I think it's a very good answer. – Gigi Oct 10 '14 at 12:45
  • The answer by @Guffa is a much better answer as it explains correctly when each should be used. – James Z. Aug 26 '15 at 23:15
98

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
  • 640,220
  • 96
  • 678
  • 956
  • This answer needs more upvote , many devs first go in to technical details of abstract and not abstract. First the semantic difference has to be understood.Abstract class shares common stuff in the same family while the interfaces can go cross family. Do check out this youtube video which goes in detail explaining things https://youtu.be/0EnSPBVrbG0 – Shivprasad Koirala May 19 '21 at 02:20
11

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
  • 24,944
  • 8
  • 81
  • 102
  • Let me do not agree with your opinion about this sentence (but inherit from another base class, use an interface.) because of inheritance = reusability. In general, interfaces don't cover reusability. When we are talking about reusability, we must have an implementation whereas in interfaces we don't have an implementation. That is why interfaces are not for multiple inheritances. – Behnam Apr 06 '20 at 17:42
8

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
  • 16,092
  • 7
  • 56
  • 86