Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 8 - multiple inheritance "abstract class"? [duplicate]

It seems to me like the C# 8.0 feature, default interface member implementation, essentially allows one to create implementations at the interface level. Pairing that with the fact that a class can implement multiple interfaces, it seems eerily close to a multiple inheritance structure for classes. As far as I understand, this seems to be quite opposite to the core of the design of the language.

Where does this discrepancy stem from and what room does this leave for actual abstract classes to occupy?

This question has been suggested as an answer to mine and while it is useful, it doesn't exactly answer my question. To be more precise:

  • I always assumed that single inheritance is one of the core principles of C#'s design, which is why the decision to implement this feature is surprising to me, and I would be interested to know where it stems from (C#-specifically).
  • The linked question does not answer what room it leaves for abstract classes.
like image 406
user622505 Avatar asked Jan 02 '23 13:01

user622505


1 Answers

I always assumed that single inheritance is one of the core principles of C#'s design

This is just not accurate. Single inheritance is a means to design goal, but not a goal in itself.

It's like saying the automatic transmission is a core design principle for car makers, when the actual goal is making the car easier and safer. And looking the car market, manual transmissions still thrive in both the low end (because they're cheaper) and the high end (performance sports cars) of the market, where they are good fit for purpose. Many models in those areas can still be had with either type of transmission.

The actual design goal in C# leading to single inheritance is more about safety and correctness with regards to memory access and overload resolution. Multiple inheritance is difficult to verify mathematically for these things compared to single inheritance. But as they find elegant solutions, C# designers have added a number of features that stretch the bounds of single inheritance. Beyond interfaces, we have partial classes, generics (and later co/contravariance), and delegate members that all trend this direction.

In this case, the default implementation is effective in safely providing a weak multiple inheritance because the inherited functionality doesn't cascade down the inheritance tree from two directions. You can't create a conflict by inheriting two different classes with differing interface implementations; you are limited to either your own class implementation, the default implementation, or the single implementation available via inheritance.

like image 157
Joel Coehoorn Avatar answered Jan 05 '23 00:01

Joel Coehoorn