workaround the lack of multiple-inheritance?

D

David

Is there a good workaround the lack of multiple-inheritance? What if some
classes expose A's properties and some B's and others expose A's and B's? I
have an A class, a B class, an AB class and an AB : C class. The AB class
has code copied into it from both A and B.

Thanks.
 
J

Jon Skeet [C# MVP]

David said:
Is there a good workaround the lack of multiple-inheritance? What if some
classes expose A's properties and some B's and others expose A's and B's? I
have an A class, a B class, an AB class and an AB : C class. The AB class
has code copied into it from both A and B.

Typically, I'd use composition. Make AB own an A and own a B, and
expose members which just delegate responsibility to those instances.
 
J

Joanna Carter [TeamB]

"David" <[email protected]> a écrit dans le message
de 5I8wf.130133$WH.87098@dukeread01...

| Is there a good workaround the lack of multiple-inheritance? What if some
| classes expose A's properties and some B's and others expose A's and B's?
I
| have an A class, a B class, an AB class and an AB : C class. The AB class
| has code copied into it from both A and B.

With your saying that AB has code copied into it from A and B, it makes me
think that this is an old VB style of "inheritance" by copy/paste :)

True inheritance never has duplicated code; it simply isn't necessay. As Jon
says, you should create a class AB that includes the behaviour of both A and
B, but you will not be able to implement polymorphic behaviour on virtual
methods from both classes of course, only one class can be your ancestor.

I would seriously recommend that you look at totally redesigning your
hierarchy, reconsidering where some of the behaviour is located and removing
any duplication.

You might also like to consider using interfaces to define the hierarchy and
then implement one or more of those interfaces in a mixture of classes.

Can you let us know a bit more on what exists and why you want to retain the
current, possibly bad, design ?

Joanna
 
J

Jeff Louie

David... There are rare times when you really need multiple inheritance
of
implementation and that can be simulated in .Net 2.0 using generics and
delegation. Most problems can be solved using interfaces and C#'s
support
for mulitple inheritance of interfaces aka pure virtual classes.

http://www.geocities.com/Jeff_Louie/oop27.htm

namespace GenericMI
{
// first we create the abstractions
public interface I1
{
void SayHello();
}
public interface I2
{
int GetValue();
}
public interface IProgram : I1, I2 { }
// second we write the concrete classes that implement the
abstractions
public class Implementation1 : I1
{
public void SayHello()
{
Console.WriteLine("Hello.");
}
}
public class Implementation2 : I2
{
private int i = 1;
public int GetValue()
{
return i;
}
}
// finally we write the generic class that contains
// the concrete classes
public class GenericMI<T1, T2> : IProgram where T1 : class, I1,
new()
where T2: class,I2, new()
{
private T1 pimplI1= new T1();
private T2 pimplI2 = new T2();
// we forward calls to the contained object
public void SayHello()
{
pimplI1.SayHello();
}
public int GetValue()
{
return pimplI2.GetValue();
}
}
class Program
{
static void Main(string[] args)
{
GenericMI<Implementation1,Implementation2> mi=
new GenericMI<Implementation1,Implementation2>();
mi.SayHello();
Console.WriteLine(mi.GetValue());
Console.ReadLine();
}
}
}

Regards,
Jeff
 
D

David

You might also like to consider using interfaces to define the hierarchy
and
then implement one or more of those interfaces in a mixture of classes.

Do you mean defining;

ABImp // no interface

class A : ABImp, IA //
{}

class B : ABImp, IB
{}

class AB : ABImp, IA, IB
{}

This may work well. Should work like Fortran's USE ONLY.
 
D

Dries

ABImp // no interface
class A : ABImp, IA //
{}

class B : ABImp, IB
{}

class AB : ABImp, IA, IB
{}

If ABImp is not an interface what is is then? A class? What is the
difference then between ABImp and AB?

If I understand you needs, all you need is

IA and IB, you can create class A, create class B, create class AB and
you can inherit from AB to create C.

greetz,
Dries
 
D

Dries

Is there a good workaround the lack of multiple-inheritance?
Be carefull with multiple-inheritance, in theorie it looks very fine, it
even more seems to solve a lot of problems. But have you ever worked
with it?

I've worked with it with Eiffel and I can garantee you that I prefer
working with interfaces and writing some more code than to debug a few
hours because of multiple inheritance that causes problems when two
classes have the same function...

Dries
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top