If default interface methods are implemented in C# 8.0 why would I ever need abstract classes? [closed]
Asked Answered
H

2

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?

Heterogeneity answered 11/8, 2017 at 18:44 Comment(4)
Abstract classes can have state. Trying to do state with default interface methods would be painful.Ambulate
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.Tensiometer
@Tensiometer Can particular interface implementer override extension method to provide more efficient implementation?Oink
@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.Tensiometer
N
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.

Namnama answered 11/8, 2017 at 19:12 Comment(1)
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.Hibbert
M
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.

Mollee answered 11/8, 2017 at 19:20 Comment(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."Heterogeneity

© 2022 - 2024 — McMap. All rights reserved.