471,605 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

workaround the lack of multiple-inheritance?

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.
Jan 8 '06 #1
6 3211
David <Co*********@AdsorptionProcessModeling.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 8 '06 #2
"David" <Co*********@AdsorptionProcessModeling.com> a écrit dans le message
de news: 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 8 '06 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Jan 8 '06 #4


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.

Jan 8 '06 #5
> 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
Jan 9 '06 #6
> 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
Jan 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by flupke | last post: by
9 posts views Thread by Philip Lawatsch | last post: by
5 posts views Thread by Charles Law | last post: by
1 post views Thread by slyi | last post: by
14 posts views Thread by Dave Booker | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.