There is an option that has been added to version 8 of C#. At that point, what will be the difference between abstract classes and interfaces and why would I ever use abstract class anymore? What would be the advantage of this change in this version?
-
2That's exactly why I don't like that feature. They are _not_ implementations in the same sense as "regular" implementations - they are _default_ implementations. They have a special purpose: You can **add** them to an _existing_ interface and thus add functionality without breaking legacy code. It is confusing and it encourages dirty coding. Me no likey :( – Fildor Mar 25 '20 at 08:21
-
I don,t think that this is dirty code, we had it before in previous versions in abstract classes so it is useful but my question is what is the advantage of this new feature ? – sajadre Mar 25 '20 at 08:31
-
I didn't say it _is_ dirty. I said it _encourages_ dirty coding. To be specific: by making easy what should be hard to do. I already went into what the feature is good for. If you want to go deeper that rabbit hole and find more valid applications: https://devblogs.microsoft.com/dotnet/default-implementations-in-interfaces/, https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions, https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/mixins-with-default-interface-methods – Fildor Mar 25 '20 at 08:35
-
The main difference is that abstract classes can have state and interfaces cant. – Magnus Mar 25 '20 at 09:16
-
Duplicate of https://stackoverflow.com/questions/47601419/default-interface-methods-what-is-deep-meaningful-difference-now-between-abstr – Peter Duniho Jul 22 '20 at 18:56
3 Answers
The fundamental feature of interfaces is still that they cannot contain fields. If you need data, you need a base class. If not an interface will probably suffice.

- 3,546
- 15
- 21
C# 8.0 introduces a new feature called Default implementations in Interfaces and this changes many things.
Interfaces can now have the default implementation of methods. Interfaces can now have Private members. Interfaces can now have static members, this is used for parameterization of the default implementation. Interfaces can now have protected members which are not accessible by the derived class but can be accessible with a derived interface. If a class wants to implement the protected member, it has to be done by implementing the interface explicitly. Interfaces can also have virtual members, but the class can’t override the method but an interface can.
We can think that Interfaces and abstract are somewhat same now, But an interface cannot have Instance state, instance fields, instance auto-properties, cannot define class level fields or variables whereas an abstract class can have state.
There are some reasons stated for this change: Extending APIs, Interoperability with Android, iOS and supporting the traits language feature.

- 610
- 6
- 18
A class can extend several interfaces but only one abstract class. A interface can not have a constructor and destructor but an abstract class can.