How can I do this:
Class A : DependencyObject {}
Class B : DependencyObject {}
Class C : A , B {}
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.
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.
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 .
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.
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(); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With