Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Private Inheritance aka C++ in C# and Why not it is present in C#

I know that private inheritance is supported in C++ and only public inheritance is supported in C#. I also came across an article which says that private inheritance usually defines a HAS-A relationship and kind of an aggregation relationship between the classes.

EDIT: C++ code for private inheritance: The "Car has-a Engine" relationship can also be expressed using private inheritance:

class Engine {
 public:
   Engine(int numCylinders);
   void start();                 // Starts this Engine
 };

class Car : private Engine {    // Car has-a Engine
 public:
   Car() : Engine(8) { }         // Initializes this Car with 8 cylinders
   using Engine::start;          // Start this Car by starting its Engine
 };

Now, Is there a way to create a HAS-A relationship between C# classes which is one of the thing that I would like to know - HOW?

Another curious question is why doesn't C# support the private (and also protected) inheritance ? - Is not supporting multiple implementation inheritance a valid reason or any other?

Is private (and protected) inheritance planned for future versions of C#?

Will supporting the private (and protected) inheritance in C# make it a better and widely used language?

like image 539
VSS Avatar asked Dec 07 '22 18:12

VSS


1 Answers

Now, Is there a way to create a HAS-A relationship between C# classes which is one of the thing that I would like to know - HOW?

Make one class have a field of the other class:

class Car
{
    Engine engine;
}

A car has an engine.

Another curious question is why doesn't C# support the private (and also protected) inheritance ?

You have a box in your basement. You have never once put anything into it. Someone asks you "why is this box empty?" What answer can you possibly give other than that the box is empty because no one ever put anything into it?

C# doesn't support private or protected inheritance because no one ever implemented that feature and shipped it to customers. Features are not implemented by default, for free. It's not like we started C# with private and protected inheritance and then took them out for some good reason. Those features were never there in the first place, and unsurprisingly, they are still not there. Features don't grow themselves.

Is not supporting multiple implementation inheritance a valid reason or any other?

I don't understand the question.

Is private (and protected) inheritance planned for future versions of C#?

No.

Will supporting the private (and protected) inheritance in C# make it a better language than it is now?

I don't think so.

One of the many problems with inheritance as we typically see it in OOP is that it utterly conflates the "is a kind of" semantic relationship with the "reuses implementation details of" mechanism relationship. Private inheritance partially addresses this problem.

There are a small number of situations in which I would have really liked to have private inheritance in C#; a recent example was that I had a data structure which was possible to construct generally, but which I did not want to expose to users:

internal class FancyDataStructure<T> {...}

but only possible to serialize when T was int (for reasons which are not germane to the discussion.) I would have liked to say:

public class SerializableThingy : private FancyDataStructure<int>

Instead I just made FancyDataStructure<T> a nested type of SerializableThingy and used composition rather than inheritance. There was a small amount of "plumbing" code to write but it worked out just fine.

I don't think adding the feature pays for itself; it adds complexity to the language in exchange for a very small benefit of avoiding some trivial plumbing taxes.

Would supporting the private (and protected) inheritance in C# make it a more widely used language than it is now?

How could I, or anyone else for that matter, possibly know the answer to that question? StackOverflow is a bad place to ask questions that require a prediction of the future based on a counterfactual. My suggestions:

  • Implement a version of C# with the feature you want. See if it becomes popular. Then you'll know.
  • Find someone with psychic powers who can predict how the future would be if things were different now. Since predictions based on counterfactuals are automatically true by the rules of deductive logic, the predictions will be accurate.
like image 93
Eric Lippert Avatar answered Dec 26 '22 10:12

Eric Lippert