4

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?

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
S2S2
  • 8,322
  • 5
  • 37
  • 65
  • 13
    Inheritance is **NOT** a **HAS-A** relationship, it is and has always been a **IS-A** relationship. – Alok Save Nov 17 '11 at 17:33
  • 4
    @Als private Inheritance is a syntactic variation of aggregation/has-a relationship - this article: http://www.parashift.com/c++-faq-lite/private-inheritance.html – S2S2 Nov 17 '11 at 17:35
  • 1
    Well, of course C# already supports a HAS-A relationship. Instead of extending the other class, you simply make a private/protected member variable containing an instance of that other class. The main benefit to C++'s private/protected inheritance was that it granted access to protected member data in that other class. – TheBuzzSaw Nov 17 '11 at 17:35
  • 2
    Scott Meyers recommends that you think of private inheritance as a "is-implemented-in-terms-of" relationship. – Kerrek SB Nov 17 '11 at 17:36
  • 1
    @Als: But private inheritance can't really be seen as an **is-a** relationship, only to `friend`s it's visible. I always see private inheritance as an **is implemented in terms of** relationship. – Xeo Nov 17 '11 at 17:37
  • 1
    http://stackoverflow.com/questions/33115/does-c-sharp-have-the-notion-of-private-and-protected-inheritance – kol Nov 17 '11 at 17:37
  • Because it is the violation of encapsulation which is the basic concept of OOD/OOP. By accessing the private members you can easy corrupt the logic in the child class and accessing the private members is permitted by good designed code. – AlexTheo Nov 17 '11 at 17:37
  • @Vijay: If your definition/Understanding of *Syntactic Variation* means **IS-A** `==` **HAS-A** then Yes, But your Understanding is flawed then. **IS-A** and **HAS-A** is not same, and neither are Private Inheritance and **HAS-A**. – Alok Save Nov 17 '11 at 17:38
  • @Als In my understanding IS-A is KIND-OF relationship and HAS-A a contains relationship – S2S2 Nov 17 '11 at 17:40
  • @Vijay: The article you quote points out more differences than similarities if you noticed,So they being same is a wrong notion. – Alok Save Nov 17 '11 at 17:42
  • @Alex: Private inheritance doesn't allow the child class to access the private members of the base class... – Xeo Nov 17 '11 at 17:43
  • @Xeo: None of Inheritances(public,protected or private) allow child class to access *private members* of the Base class. – Alok Save Nov 17 '11 at 17:45
  • @Als: I know that, but it seemed Alex understood something different under it than you and me. – Xeo Nov 17 '11 at 18:02

2 Answers2

14

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.
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Its written in this C++ article: http://www.parashift.com/c++-faq-lite/private-inheritance.html that private inheritance variant allows access to the protected members of the base class, I am not sure if it would be possible using instance field resolution of a contained class in a containing class- in C#. Can you please explain this point? – S2S2 Nov 17 '11 at 18:11
  • 3
    @Vijay: Well that's easily solved. If B has a protected field F and you want to access it from D which contains a B then derive C from B, have C expose F, and have D contain a C. Now D can see F, no problem. "protected" is a misnomer; nothing is actually "protected" from inspection. "protected" is just "public" with a thin veneer overlaid upon it. – Eric Lippert Nov 17 '11 at 18:22
  • Thanks @EricLippert this is what I was thinking but wasnt sure, to expose protected using containment.. – S2S2 Nov 17 '11 at 19:41
1

You would create a Has-a relationship in the normal way: Have one class contain an instance member of another class.

In C++, private inheritance is occasionally used to represent an implemented-in-terms-of relationship, typically for simplicity in gaining access to the parent methods/interface. It's probably a rare enough use that I wouldn't be too concerned that C# doesn't offer it, especially when composition offers fairly similar functionality.

Mark B
  • 95,107
  • 10
  • 109
  • 188