-1

With the recent release of C# 8.0 (new feature mentioned here), it allows for providing default method implementation.

While I understand the reason why it's done, I wonder if there is any difference between these 2 concepts?

Thoughts?

  • 3
    I have pointed this out to my developer coworkers. That essential every interface has become an abstract class. Thus the new feature, while I understand it.....irks my OO purist sensibilities ! – granadaCoder Jul 22 '20 at 18:37
  • Whats non OO in multi inheritance? C++ has it – Legacy Code Jul 22 '20 at 18:47
  • @LegacyCode, I LOL'ed and definitely want to upvote your answer for the humor... Didn't do it as this is stack overflow :D. Don't want to misguide others into the comment :P – Chetan Nadgouda Jul 22 '20 at 21:49
  • For those who come after this question, looking for answers... more reading material (slightly tangential) is https://github.com/dotnet/csharplang/issues/288 – Chetan Nadgouda Jul 22 '20 at 21:57

2 Answers2

1
  1. To call default interface method You need to cast instance to the interface containing default implementation
  2. You can implement multiple interfaces and cannot inherit multiple abstract classes

It is better to use abstract class for base functionality (provides better inheritance tree logic). And use interfaces with default method implementations to provide small side changes only when this default logic suits for most of implementations (like Microsoft did with their Collection methods).

Also try to not overuse default interface implementations, because this complicates behavior inheritance (you need to search interfaces too, not only classes) from linear to tree.

C++ allows multiple inheritance and without control this aspect can easily make the code unmaintainable.

satma0745
  • 667
  • 1
  • 7
  • 26
  • If MI was the only issue, then it could be solved by using composition. Why *change* the interfaces? There has to be more to it.... – Chetan Nadgouda Jul 22 '20 at 21:46
  • 1
    Imagine You have some small interface (1-2 methods). And it is implemented by many classes. Suppose, You have noticed that most of implementations are the same. And You have two options: introduce micro-class that implements this interface and stored as field in destination classes; use default interface implementation. In this case without default interface implementation You need to write a lot of boilerplate code. Default interface implementation is just a handy pretty dangerous(remember multi inheritance problem) feature, that can make Your life easier if You not overuse it. – satma0745 Jul 23 '20 at 07:33
1

These are the main differences I see:

Multiple Inheritance

Unlike other languages (like C++), C# does not allow a class to inherit directly from more that one class. On the other hand, a class can implement any number of interfaces, so the new default implementation feature allows you to do something similar to multi-inheritance.

Calling the Default Implementation

If you derive from a class and override a virtual method, you may use the base keyword to call the original implementation. Example:

public abstract class Base
{
    public virtual void DoSomething()
    {
        ...
    }
}
public class Derived : Base
{
    public override void DoSomething()
    {
        base.DoSomething(); //Here
        ...
    }
}

Constructors and Non-Virtual Members

Abstract classes can declare constructors that the derived class will call. Also, an abstract class can contain non-virtual methods, while an interface can only have overridable (virtual) methods.

Gur Galler
  • 820
  • 9
  • 21