Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

as mostly all of us heard and most of us believe that .net does not supporting multiple inheritance (if we want to perform that then we have to use interface for that) if it is really true then anyone can explain me and help me to understand what is happening in the following scenario:

as we all know that every class created in .net is inheriting System.Data by default so i have created following class:

C#
class MyCls1
   {
       public void display()
       {
           Console.WriteLine("Hello");
       }
   }


where i have defined only single methood "display". when i create the object of MyCls1 class and try to call the display() methood, i have got "Equals", "GetHashCode", "GetType" and "ToString" methoods also (which is derrieved from system.object may be).

now i have created another class named "Mycls2" and same thing is haapen with it also.

C#
class Mycls2
   {
       public void Show()
       {
           Console.WriteLine("Hello2");
       }
   }


so we can say that these two classes inheriting System.Object class.
now if i inherit class MyCls1 into class Mycls2, so bacause of class Mycls2 is already inheriting System.object and now its inheriting class MyCls1 also so that can we say that class MyCls2 is inheriting both class MyCls1 and System.object at same time (that is what happen in the multiple inheritance) and it is in the multiple inheritance ??? and if its not then why ???

C#
class MyCls1
   {
       public void disp()
       {
           Console.WriteLine("Hello");
       }
   }

   class Mycls2 :MyCls1
   {
       public void Show()
       {
           Console.WriteLine("Hello2");
       }
   }
Posted

That isn't multiple inheritance.
Think about it for a moment, and look at families or people.
You are derived from your parents: and they are derived from their parents. Which means that you "inherit" from both your parents, and your grandparents. And from their parents, and theirs, and so on...

That isn't multiple inheritance, it's inheriting from the "whole branch" that makes up your parent.

Class inheritance is the same:
Object -> Control -> Form -> MyForm 
means that all the properties of Control and Object are available in MyForm because it inherits the whole branch - otherwise you couldn't design anything complex, because the whole hierarchy would have to be "flat":
Object -> Everything
Then true inheritance wouldn't work at all.

Multiple inheritance is when you inherit from unrelated branches:
object -> Control - > Form
object -> Collection -> List
Class MyClass: Form, List
And C# does not allow that.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Mar-14 5:52am    
Bad example about human parents. This inheritance is exactly multiple. Not understanding of this simple fact really creates misnomers like "family tree" (it cannot possibly be a tree, unless one of parents is ignored).

And you forgot to mention "weak form of multiple inheritance". Even though OP asked about classes, the general question was "is multiple inheritance possible in c#". Yes, it is possible, but only for interfaces...

—SA
Multiple inheritance is actually allowed in C#, but only for interfaces. Also, inheritance list for a file can include only one file but any number of interfaces. This is sometimes called "weak form of multiple inheritance". Indeed, it's much weaker then free multiple inheritance. But, in certain sense, creates less problems.

—SA
 
Share this answer
 
C# implements single inheritance but allows for multiple interfaces at the same time.
Quote:
The biggest problem with multiple-inheritance is that it allows for ambiguity when the compiler needs to find the correct implementation of a virtual method.


Read this : Multiple Inheritance in C#[^]
Why it is not allowed : (MSDN[^])
1. Different languages actually have different expectations for how MI works. For example, how conflicts are resolved and whether duplicate bases are merged or redundant. Before we can implement MI in the CLR, we have to do a survey of all the languages, figure out the common concepts, and decide how to express them in a language-neutral manner. We would also have to decide whether MI belongs in the CLS and what this would mean for languages that don't want this concept (presumably VB.NET, for example). Of course, that's the business we are in as a common language runtime, but we haven't got around to doing it for MI yet.

2. The number of places where MI is truly appropriate is actually quite small. In many cases, multiple interface inheritance can get the job done instead. In other cases, you may be able to use encapsulation and delegation. If we were to add a slightly different construct, like mixins, would that actually be more powerful?

3. Multiple implementation inheritance injects a lot of complexity into the implementation. This complexity impacts casting, layout, dispatch, field access, serialization, identity comparisons, verifiability, reflection, generics, and probably lots of other places.

-KR
 
Share this answer
 
Comments
WarLock007 10-Mar-14 2:27am    
hi Kunal,
thnx for a nice and detailed answer, but still i have one doubt in my mind that is: because both class MyCls1 and class MyCls2 are inheriting System.object class so that both have the same copy of methoods ("Equals", "GetHashCode", "GetType" and "ToString" ) inherited from System.object, but when we inheriting class MyCls1 into class MyCls2 then class MyCls2 will have the multiple copies of the above said methods (one from direct System.object and the other one is from class MyCls1) so when we will create the object and call above said methods then how the compiler is dealing with those ambiguous method callings.
(Ex: the same issue occurs in c++ and resolved by using virtual inheritance)
http://www.cprogramming.com/tutorial/virtual_inheritance.html
Krunal Rohit 10-Mar-14 2:31am    
That would be overriding.

-KR
WarLock007 12-Mar-14 7:09am    
that is not overriding because in overriding its required that the methods of the base class (that need to be override) should be re implemented in the child class. but here in my code i have not done this.
sorry but that's not the answer i m searching for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900