0

I have 2 classes base1 and base2

   public class base1
    {
public base1(int I, string s)
{
}
      public void f1()
    {
    // code
    }
    public void f2()
    {
    // code
    }
    public void p()
    {
    // code
    }

    }
    public class base2
    {
public base2(int x)
{
}
      public void f3()
    {
    // code
    }
    public void f4()
    {
    // code
    }

    protected void f5()
    {
    // code
    }

    }

I need to implement 5 classes that derive from both of them but there is no multiple inheritance in c#....so how can I do it?-one class base1i can`t change. with the second one i can expirement.so the only idea i have is to make it to be interface? but both classes constructors have arguments!so it is not possible to make a interface may be you have better ideas...

YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • Interesting as well http://stackoverflow.com/questions/2515886/design-pattern-to-use-instead-of-multiple-inheritance – Kilazur May 25 '14 at 06:48
  • Like many have mentioned, multiple inheritance is not preferable... it's actually pretty dangerous. When I studied C++, I learned about the Diamond problem of inheritance. Basically, you may end up with a situation where you're calling a method of the same name that exists in both base classes. There's a solution to it using a virtual keyword, but it's all a mess in my eyes. – B.K. May 25 '14 at 07:30
  • @Sajeetharan-no.look ataupdate about constructor – YAKOVM May 25 '14 at 07:34

3 Answers3

1

Multiple inheritance had been ruled out for C# after careful study. Yes, Interfaces are indeed the recommended way to go. That is unless you can use Composition. It really depends on what behaviour and what knowledge the classes are supposed to model!

TaW
  • 53,122
  • 8
  • 69
  • 111
0

There is no better idea, as you said, in C# you can inherit only from one class .

You should extract an interface from your classes and inherit from these interfaces .

jony89
  • 5,155
  • 3
  • 30
  • 40
0

I recommend to extract the functionality to interfaces, and then, to deal with the parameters in constructor, create a new method inside the interfaces called, Load or something, that takes the parameters that the constructor takes right now.

public Interface IBase1
{
    public void Load(int param1, string param2);

    public void f1();

    public void f2();

    //The rest of methods...
}

And the same with the second one!

Then you class will inherit from this interface, and will fill the Load method.

public class MyClass : IBase1
{
    public void Load(int param1, string param2)
    {
    // Implementation
    }
}

I hope this helps for you

Oscar Bralo
  • 1,912
  • 13
  • 12