Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inherit from two classes in C# [duplicate]

Tags:

Possible Duplicate:
Multiple Inheritance in C#

I have two classes Class A and Class B. These two classes cannot inherit each other. I am creating new class called Class C. Now, I want to implement the methods in class A and class B by inheriting. I am aware that multiple inheritance is not possible in C# but is there any other way to do this?

like image 628
Virus Avatar asked Jun 21 '12 09:06

Virus


People also ask

Can you inherit from 2 classes C#?

C# does not support multiple inheritance , because they reasoned that adding multiple inheritance added too much complexity to C# while providing too little benefit. In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance .

When a class inherits from two or more classes it is called?

When a class is derived from more than one base class it is called multiple Inheritance.

How do I inherit from two classes?

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.

Can a class be inherited from 2 classes?

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.


2 Answers

Multitiple inheritance is not possible in C#, however it can be simulated using interfaces, see Simulated Multiple Inheritance Pattern for C#.

The basic idea is to define an interface for the members on class B that you wish to access (call it IB), and then have C inherit from A and implement IB by internally storing an instance of B, for example:

class C : A, IB {     private B _b = new B();      // IB members     public void SomeMethod()     {         _b.SomeMethod();     } } 

There are also a couple of other alternaitve patterns explained on that page.

like image 196
KF2 Avatar answered Sep 19 '22 15:09

KF2


An common alternative to inheritance is delegation (also called composition): X "has a" Y rather than X "is a" Y. So if A has functionality for dealing with Foos, and B has functionality for dealing with Bars, and you want both in C, then something like this:

public class A() {   private FooManager fooManager = new FooManager(); // (or inject, if you have IoC)    public void handleFoo(Foo foo) {     fooManager.handleFoo(foo);   } }  public class B() {   private BarManager barManager = new BarManager(); // (or inject, if you have IoC)    public void handleBar(Bar bar) {     barManager.handleBar(bar);   } }  public class C() {   private FooManager fooManager = new FooManager(); // (or inject, if you have IoC)   private BarManager barManager = new BarManager(); // (or inject, if you have IoC)    ... etc } 
like image 33
Rich Avatar answered Sep 18 '22 15:09

Rich