0

How to use multiple inheritance in c#?.how it is possible using interface?

in c# doesn't support multiple inheritance. so we use interface. how could be use them. how it is working?

27th Mar 2017, 6:03 PM
saravanan
saravanan - avatar
1 Odpowiedź
0
1. The Problem: which? class A { public void DoA() { ... } public void DoX() { ... } } class B { public void DoB() { ... } public void DoX() { ... } } class C : A, B { } C = new C(); C.DoX(); //<-- what will happen here? Classes do inherit all (non-static) methods from their parents. And so, C will have a DoA(), DoB() and DoX() methods. The inherited methods once called run the method in the parent where from it is inherited (if not overridden). Here, C.DoX() should call the <Parent>.DoX(), but there are two methods matching the definition: A.DoX() and B.DoX(), complitely different codes. Which will be called? Both? In which order? (How) is it logical? 2. Solution: Interfaces Interfaces do not define the code of the methods, only their signature (name and parameter types). So, they require to be defined later on, in the implementer class, or in its only one class parent, if there is. Which means, you can implent any number of interfaces, there will be no collision between them. class A { public void DoA() { ... } public void DoX() { ... } } interface B { void DoB(); void DoX(); } class C : A, B { public override void DoB() { ... } }
7th Apr 2017, 1:28 PM
Magyar Dávid
Magyar Dávid - avatar