Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does C# support multiple inheritance? Either way give some example.
Thanks.
Posted

Only a weak form of multiple inheritance: strictly one parent class, but multiple interfaces are allowed. The only exclusion is the class System.Object which does not have a base class. If no base class is specified, it means that a base class still exist; this implied base class is System.Object.

C#
interface MyInterface1 { void Method(); }
interface MyInterface2 { void Method(); }

//this is fine:
class MyClass : System.Windows.Form, MyInterface1, MyInterface2 {
    void MyInterface1.Method() {}
    void MyInterface2.Method() {}
}

//this will not compile, failed at attempt to add another base class MyClass:
class MyClass : System.Windows.Form, MyClass, MyInterface1 {
    void MyInterface1.Method() {}
}


—SA
 
Share this answer
 
v3
Comments
Dima Popov 3-Mar-11 16:19pm    
Surely a better answer.
Sergey Alexandrovich Kryukov 3-Mar-11 16:22pm    
Thank you.
--SA
AHBB 3-Mar-11 16:20pm    
class P:c1 //how to call c2 & c3 after c1?
{

}
class c1
{ }
class c2
{ }
class c3
{ }
Sergey Alexandrovich Kryukov 3-Mar-11 16:23pm    
Incorrect question. Classes are not called, method and properties are (yes, properties, too).
If you question is about the order of declaration -- it does not matter. This is not C/C++!

--SA
AHBB 3-Mar-11 16:44pm    
thanks
We can use multiple inheritance in c++ but C# never support for multiple inheritance. Instead of that we can use interface concept in C#.
 
Share this answer
 
No, but internally every class inherits object.
 
Share this answer
 
Comments
AHBB 3-Mar-11 16:16pm    
no clear
Sergey Alexandrovich Kryukov 3-Mar-11 16:17pm    
Incomplete answer, also, you "but" is irrelevant.
--SA
Dima Popov 3-Mar-11 16:23pm    
Is that just irrelevant or technically incorrect?
Sergey Alexandrovich Kryukov 3-Mar-11 22:04pm    
Dima, "No" is questionable, because of the term "weak form of multiple inheritance". You see, technically interface layout matches the layout of the class with no data and all abstract method (as it was historically implemented this way and makes full sense); I mean "weak form of multiple inheritance" is still multiple inheritance. Enough said. As to the System.Object as a common ancestor -- it is the unrelated fact. One can create systems with singular or multiple inheritance and with single or multiple common ancestors in all combination without interference between these two factors -- these aspects are orthogonal to each other.

--SA
Sergey Alexandrovich Kryukov 4-Mar-11 0:16am    
I'm sorry. Mentioning of System.Object still makes the following sense: in the sense of "multiple inheritance" what multiplicity is applied to a .NET class? 0..INF for base interfaces, strictly 1 (not 0) for base classes. Why not 0? Because if no base class is declared, there is an implied base class which is System.Object.
"every class inherits object" in not quite accurate, because you need to make difference between direct and indirect inheritance. In the indirect sense, this can be inferred from the above rule.
--SA
This[^] might give you an idea.

HONESTLY SAY NO, USE INTERFACE INSTEAD OF MULTIPLE INHERITANCE.
 
Share this answer
 
v2
Did you try it? 5 minutes of coding would have answered the question. I'm sure your teacher wants you to find out yourself.
 
Share this answer
 

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