50

Suppose we have two methods M1() and M2() in an interface. An abstract class also has just the same two abstract methods. If any class implemented this interface or inherited from the abstract class, it will have to implement both the methods in it.

So to me, it seems that an interface or an abstract class behaves the same for my scenario. So, can anyone highlight the difference between these two in this specific case and suggest whether to use an abstract class or an interface here?

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
WpfBee
  • 2,837
  • 6
  • 23
  • 29
  • Ha! maybe we could combine all our simultaneous ansers and share the rep. ;-) – Pieter Geerkens Mar 02 '13 at 19:14
  • Mate & llya can you please elaborate your thoughts? Suppose I have an interface IMath that contains two methods AddNumbers() & ConcatenateStrings(). These two as abstract methods are also in an abstract class Math. Now can you please suggest should I use IMath or Math? – WpfBee Mar 02 '13 at 19:19
  • https://stackoverflow.com/a/74781277/7186739 – Billu Dec 13 '22 at 07:27

5 Answers5

137

There are technical differences between Abstract Classes and Interfaces, that being an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes. A class can implement multiple interfaces, but it can only inherit one class (abstract or otherwise).

However, in my opinion, the most important difference between Interfaces and Abstract Classes is the semantic difference.

An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is.

Take for example IEnumerable, the semantic meaning behind this is that anything that implements IEnumerable is enumerable, it doesn't mean that it's an enumeration, but that it can behave like one (can be enumerated).

Contrast that with a washing machine example, anything that inherits it is a type of washing machine. Anything that inherits it would be a type of washing machine, a top loader, or side loader, etc.

Instead, if you had an interface called ICanWash, which could contain a method called Wash. You could have various things implement ICanWash, be it a Person, an abstract washing machine class, etc, where the actual implementation does not matter, just you need to know that the behavior is that it can wash things.

In summary, classes define what something is, interfaces define what something can do.

Matthew
  • 24,703
  • 9
  • 76
  • 110
  • 1
    This is what I was looking for. Very good point, since that's what's OOP is all about I think. – marsze Mar 16 '17 at 07:51
3

From MSDN:

By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes

So, use interface if you want that any class can inherit that methods.

From same MSDN page:

In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Suppose currently, there is not a clear requirement about it's inheritance then which one to prefer? – WpfBee Mar 02 '13 at 19:26
  • 1
    More examples: if you want create a methods/propeties which other classes can implement then use interface. But remember when you add/remove methods in interface, then you must do the same in all classes which implements that. In abstract classes you can add/remove methods and this affect all classes which inherit a abstract class – Fabio Mar 02 '13 at 20:16
1

Interface allows a class to inherit/implement more than one interface, while in C# you can only inherit from one class.

Multiple inheritance, basically.

CharlesW
  • 625
  • 6
  • 14
  • 1
    So, final take for me from this discussion: 1. An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is. 2. Whenever possible use interfaces and not abstract classes. Reason: if you have a class that has already implemented some interfaces & inherit one class. So, in this case this class will not be able to inherit your abstract class as multiple inheritance is restricted. But if it wants to implement interface then there is no problem. – WpfBee Mar 06 '13 at 16:10
  • 1
    Pretty much correct, but if you implement an interface (in class declaration), you have to code it - there is no choice. Just clarifying that just in case. – CharlesW Mar 14 '13 at 16:08
1

Abstract class not only holds abstract methods, it may also hold other fields and methods with implemntation. In c# you can not inherit from multipule classes, but you can implement multipule interfaces. So the short answer is: whenever possible use interfaces and not abstract classes. In your example it is possibe- and therfor recomended to use an interface.

omer schleifer
  • 3,897
  • 5
  • 31
  • 42
1

Two quick thoughts on differences between interfaces and abstract classes:

  1. Abstract classes desired if future expansion is likely, as an abstract class can be expanded, but an interface would have to be enhanced by addition of another interface, I2.
  2. Single (implementation) inheritance means chooses abstract classes careully, to most closely reflect the true basic nature. Interfaces can easly be added to an implemntation, but an abstract class can only be added if there is not one already.
Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52