-2

As per my previous knowledge, I always come to know that multiple inheritance is not possible in .NET. But if it is true, then I am confused when I have following code segment..

Suppose I have a class named Employee

class Employee{}

Then I am inheriting Employee class in another class named Manager

class Manager : Employee
{

}

Our every class, either defined in .NET class library or user-defined are always have System.Object as its base class. If it is true, then it means Manager class have two base classes, one is System.Object and another is Employee, where System.Object is being inherited implicitly and Employee class is being inherited explicitly, and Manager class has two base classes which forms Multiple Inheritance in place, which is actually impossible in .NET.

So, we can say that somewhere/somehow, multiple inheritance is possible, and if it is possible then why there is compile-time error when developers write code for it.

dbugger
  • 15,868
  • 9
  • 31
  • 33
MehraG
  • 15
  • 4
  • Multiple inheritance not directly possible in .NET. – Dilip Oganiya Apr 22 '16 at 13:55
  • 1
    You are confused -- this is not multiple inheritance. Manager has one base class: Employee. Employee has one base class System.Object. Manager gets the goodies of System.Object by inheriting from Employee, it is not magically inheriting System.Object implicitly. – dbugger Apr 22 '16 at 13:56
  • 4
    You seem to have misunderstood what multiple inheritance means. We talk about multiple inheritance when a class has 2 (or more) direct parent classes. A chain of classes inheriting from each other is still single inheritance regardless of how long the chain is. – laszlok Apr 22 '16 at 14:37

2 Answers2

0

C# only allows single inheritance. The System.Object class is inherited implicitly by your Employee class. So Manager class is-a Employee class, which is-a System.Object.

The compiler will handle this so you don't need to explicitly say that Class Employee : System.Object.

In summary, every class inherits from System.Object. In case of Application level inheritance, only the top most class gets the System.Object as a base class.

KevDev
  • 336
  • 1
  • 8
  • KevDev, Somewhere your last line (In Summary, ... as a base class) justifies my question but can you please give me a link or document that verifies this statement. – MehraG Apr 22 '16 at 14:03
  • The object browser will show it very clearly. – dbugger Apr 22 '16 at 14:05
  • 1
    The point here is that System.Object is the [ultimate **base** class](https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx). Classes can inherit from one other class, but the base application level class will always inherit from System.Object – KevDev Apr 22 '16 at 14:23
0

As KevDev and other users have stated, single inheritance is only possible in .NET. MSDN states that the Object class is "This is the ultimate base class of all classes in the .NET Framework".

This is what single inheritance looks like:

Single inheritance

Notice the ** single direct** link between each class. Now here is what multiple inheritance looks like:

Multiple inheritance

Note that in the first picture, all classes are derived from the "super" or "base" class Shape. In the second picture, Class C inherits from both Class A AND Class B at the same time.

Try and think of the structure like this: For single inheritance, imagine you have a single line of people at a bus station. The gates (usually) only allow one person through at a time. Now if the gate is wide open, multiple people are able to walk through. Each person resembles one class.

Sean
  • 507
  • 1
  • 9
  • 27