Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate multiple inheritance in C#

How can I do this:

Class A : DependencyObject {}

Class B : DependencyObject {}

Class C : A , B {}
like image 607
Delta Avatar asked Jul 08 '10 00:07

Delta


People also ask

How can we apply multiple inheritance?

The only way to implement multiple inheritance is to implement multiple interfaces in a class. In java, one class can implements two or more interfaces. This also does not cause any ambiguity because all methods declared in interfaces are implemented in class.

Does C support multiple inheritance?

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B. But C# does not support multiple class inheritance.

What is alternative for multiple inheritance?

Composition and Interface Inheritance are the usual alternatives to classical multiple inheritance. Everything that you described above that starts with the word "can" is a capability that can be represented with an interface, as in ICanBuild , or ICanFarm .

What is multiple inheritance example?

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.


1 Answers

C# does not have multiple inheritance, so the line

Class C : A , B {}

will never work. You can do similar things with interfaces though, along the lines of

interface InterfaceA { void doA(); } 
class A : InterfaceA { public void doA() {} }

interface InterfaceB { void doB(); }
class B : InterfaceB { public void doB() {}}

class C : InterfaceA, InterfaceB
{
  m_A = new A();
  m_B = new B();

  public void doA() { m_A.doA(); }
  public void doB() { m_B.doB(); } 
}
like image 106
Nicholas M T Elliott Avatar answered Oct 24 '22 15:10

Nicholas M T Elliott