7

I recently ran into a list of features that are being considered for addition in the next version of C#. One of them is called "default interface methods":

https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md

In short, it will allow you to define actual method implementations on the interfaces themselves meaning interfaces can now have implementations. Since this is the case, and C# classes can implement/inherit from multiple interfaces then why in the world would I ever use abstract classes?

The only thing that comes to my mind is that interfaces cannot have a constructor so maybe there is a need to run some logic in the abstract class constructor and that would justify defining an abstract class.

Is there any other scenario that anyone can think of?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Marko
  • 12,543
  • 10
  • 48
  • 58
  • 4
    Abstract classes can have state. Trying to do state with default interface methods would be painful. – willaien Aug 11 '17 at 18:54
  • This isn't meaningfully different from the ability to write extension methods for an interface that has been around since C# 3.0. Any reason you've ever had to create an abstract class since then would still exist. – Servy Aug 11 '17 at 19:14
  • @Servy Can particular interface implementer override extension method to provide more efficient implementation? – user4003407 Aug 12 '17 at 03:02
  • @PetSerAl Not through virtual dispatch. They can statically. If you want the different implementations to provide their own implementation then it belongs on the interface itself. – Servy Aug 13 '17 at 21:36

2 Answers2

1

Usually class inheritance feels like a taxonomy (x 'is a' y), while interfaces are behaviours (x 'can do' y), so there is a subtle difference in implied meaning. Although you're right that the technical distinction is less clear cut.

matt_t_gregg
  • 192
  • 9
  • A lot of new C# features tend to make lines blurry. For example, `GetValue() { return x; }` and `Value { get { return x; } }` didn't seem to similar, but then, with expression bodies, `GetValue() => x;` and `Value => x;`. All of a sudden, it seems like the only difference is that `GetValue()` has to use the word `Get` because it's a function and needs a verb, and also has to use parenthesis, but besides that, they're more or less the same. – AustinWBryan May 09 '18 at 23:01
1

Apart from state as mentioned in comments,

Base Class

You can't inherit an interface from a Base class. Interface can only inherit an interface. You would need abstract class to derive from some other class. And since you can't inherit from class, you can't override class methods. Which you can override in abstract class.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • 1
    In that link to the feature, under "static and private methods" section, they specifiy that they will allow for interface to declare private methods: "Because interfaces may now contain executable code, it is useful to abstract common code into private and static methods. We now permit these in interfaces." – Marko Aug 11 '17 at 19:24