-3

When we create a type in C#, i.e., A class, it automatically inherits from object.

Now this class can be inherited by another class.

So we have a hierarchy like this:

object : Class A : Class B

Technically Class B is inheriting Class A and object.

When this is possible, why is this not:

class C : A, B
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Codehelp
  • 4,157
  • 9
  • 59
  • 96
  • 2
    I think you can look this : https://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c – Ivan Jelev Dec 20 '17 at 05:42
  • Could you please clarify what you are trying to ask? Clearly you know that C# does not support multiple inheritance... So what exactly you looking for in this question? – Alexei Levenkov Dec 20 '17 at 05:43
  • 3
    The first part of your question doesn't have any bearing on the second. You describe single inheritance, then ask why multiple inheritance isn't allowed. `Technically Class B is inheriting Class A and object.` in a hierarchy, not at the same time. – Evan Trimboli Dec 20 '17 at 05:43
  • Your first example is allowed in the C# spec. The second is not. – Enigmativity Dec 20 '17 at 05:45
  • @EvanTrimboli it does. I am trying to understand when every class can inherit from object ideally any user crated class when inherited by another class is already inheriting from two classes (object and Class A). – Codehelp Dec 20 '17 at 05:47
  • 1
    "is already inheriting from two classes (object and Class A)". It's not. A inherits from object. B inherits from A. – Evan Trimboli Dec 20 '17 at 05:47
  • 2
    @Codehelp If my father inherits money from his father when he dies, he is free to do whatever he wants with that money when he dies, right? If he elects to leave all of his belongings to me, I get his belongings and the money that he inherited, right? That isn't the same as both of them electing to leave money to me, is it? By the same logic, `ClassB` is only inheriting from `ClassA`, and `ClassA` is inheriting from `object`. `ClassB` still gets all of `object`, but this is not multiple inheritance. – ProgrammingLlama Dec 20 '17 at 06:09

2 Answers2

2

Taking cue from your example, Class A inherits Object class which in turn is inherited by Class B it is not multiple inheritance but multilevel inheritence. In case you override a function in class A it is the overrided function which will be inherited in class B not the function of class A

Inheritance example

I took this image from this page https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

BlindSniper
  • 1,731
  • 3
  • 16
  • 30
0

No; Multi-class inheritance (multiple inheritance) is not supported in C#.

Yes; Every type in .NET derives from Object (directly or indirectly).

The way you are understanding this is incorrect; this is NOT multi-class inheritance. This is multi-level inheritance.

Take the following example:

class A : Object

class B : A //This is what happens = Correct = Multi-level inheritance

class B : A, Object//This is what you are thinking = Incorrect = Multi-class inheritance.

Yes, class A derives from Object.
Yes, class B derives from A; but class B does NOT directly derive from Object.

As class A is derived from Object, all exposed members of Object are accessible to A. As class B is derived from A, all exposed members of A are accessible to B. That is why, all exposed members of Object are also accessible to class B.

This way, class B is also derived from Object indirectly. This is multi-level inheritance.

That is why your following statement is correct:

Class B also derives from object. Every type in .NET does.

Just a slight correction:

All types in .NET are directly derived from Object if they are not derived from another type. If they are derived from another type, then they are indirectly derived from Object. Yes, in any case, each type is derived from Object.

Community
  • 1
  • 1
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141