473,748 Members | 7,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to do a multiple inheritance - like

hi

i have 2 classes A1 and A2 implementing a problem with 2 different
ways
i also have 2 other classes X1 and X2 implementing an other problem

i need classes that provide A1+X1 methods, A1+X2, A2+X1 and A2+X2:

interface A
class A1 implements A
class A2 implements A
interface X
class X1 implements X
class X2 implements X

(note: each class add custom public methods that cannot be in the
interface.

class A1X1 inherits A1, inherits X1
class A1X2 inherits A1, inherits X2
class A2X1 inherits A2, inherits X1
class A2X2 inherits A2, inherits X2

In pure C++ it was realy simple, but in C# (and VB) I cannot do that!
and i cannot use C++ in my project.

The only way to have multiple inheritance is to use interface... but
where is the interest to write 4 times the same code in
implementation! !!?

I like to use classes to store function to not write them sevral
times, so i don t think using inheritance with interfaces is a good
idea (because interface doesn t containt code).
I'm used to have interfaces in C++ on top of my hierarchy and have
multiple inheritance between lower classes and i cannot do this now !
I know it s the same problem in java so there should be a way (a
receipe?) to avoid multiple inheritance.
Nov 16 '05 #1
8 2209
Gaetan wrote:
hi

i have 2 classes A1 and A2 implementing a problem with 2 different
ways
i also have 2 other classes X1 and X2 implementing an other problem

i need classes that provide A1+X1 methods, A1+X2, A2+X1 and A2+X2:

interface A
class A1 implements A
class A2 implements A
interface X
class X1 implements X
class X2 implements X

(note: each class add custom public methods that cannot be in the
interface.

class A1X1 inherits A1, inherits X1
class A1X2 inherits A1, inherits X2
class A2X1 inherits A2, inherits X1
class A2X2 inherits A2, inherits X2

In pure C++ it was realy simple, but in C# (and VB) I cannot do that!
and i cannot use C++ in my project.

The only way to have multiple inheritance is to use interface... but
where is the interest to write 4 times the same code in
implementation! !!?

I like to use classes to store function to not write them sevral
times, so i don t think using inheritance with interfaces is a good
idea (because interface doesn t containt code).
I'm used to have interfaces in C++ on top of my hierarchy and have
multiple inheritance between lower classes and i cannot do this now !
I know it s the same problem in java so there should be a way (a
receipe?) to avoid multiple inheritance.


What you experience is indeed a thing that some people consider a 'flaw' in
..NET: lack of multiple implementation inheritance. (I'm one of them). You can
somewhat work around this using aggregation. In A1X1, you inherit from A1 and
implement X again, but you also enclose an instance of X1, so your X
implementation is a wrapper (stub) which simply calls X1 methods. With the
interface stubber in VS.NET 2003, not that hard to do, and you save yourself
double implementations of the real logic. Not that great, but workable.

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2

"Gaetan" wrote...
i have 2 classes A1 and A2 implementing a
problem with 2 different ways
i also have 2 other classes X1 and X2 implementing
an other problem

i need classes that provide A1+X1 methods,
A1+X2, A2+X1 and A2+X2:

interface A
class A1 implements A
class A2 implements A

interface X
class X1 implements X
class X2 implements X

(note: each class add custom public methods
that cannot be in the interface.

class A1X1 inherits A1, inherits X1
class A1X2 inherits A1, inherits X2
class A2X1 inherits A2, inherits X1
class A2X2 inherits A2, inherits X2
The only way to have multiple inheritance is to use
interface... but where is the interest to write 4
times the same code in implementation! !!?
You don't have to write the implementation more than once.
I know it s the same problem in java so there should
be a way (a receipe?) to avoid multiple inheritance.


A common approach is to use composition:

class A1X1 implements A, X
{
private A1 a1...
private X1 x1...

// and the "implementation " in A1X1 only passes
// the calls to the included instances
}

....or:

class A1X1 extends A1 implements X
{
private X1 x1...

// and the "implementation " for X only passes
// the calls to the instance above
}

....etc.

There are many more ways to achieve this...

The pattern you described above also suggests that you possibly could
benefit from some Factory-pattern, but that actually depends on how these
classes actually will be used.

// Bjorn A
Nov 16 '05 #3
"Gaetan" <ga****@xeberon .net> wrote in
news:97******** *************** ***@posting.goo gle.com...
...
I know it s the same problem in java so there should be a way (a
receipe?) to avoid multiple inheritance.


Yes: It's called "software design".
If you need the functionality of two different classes, create two instances
of those classes and call methods these. If they need to communicate with
each other to fullfil their job, create an interface to define what
communication may happen.
In 99.9% of the cases this approach will produce better code than using
multiple inheritance.
If you think you have a case out of those other 0.1% - please post it!

Niki
Nov 16 '05 #4
Niki Estner wrote:
"Gaetan" <ga****@xeberon .net> wrote in
news:97******** *************** ***@posting.goo gle.com...
...
I know it s the same problem in java so there should be a way (a
receipe?) to avoid multiple inheritance.


Yes: It's called "software design".
If you need the functionality of two different classes, create two instances
of those classes and call methods these. If they need to communicate with
each other to fullfil their job, create an interface to define what
communication may happen.
In 99.9% of the cases this approach will produce better code than using
multiple inheritance.
If you think you have a case out of those other 0.1% - please post it!


I can think of a lot of cases :) For example every I*able interface
describes behaviour. If you could implement an abstract implementation of
that interface which is written using for example the strategy pattern, you
can create classes which should support a given behaviour very fast by
multiple inheriting from these abstract implementations , only filling in the
blanks :)

In .NET there is another example: a class which is a collection class and
which also should support COM+. It therefore has to inherit from
ServicedCompone nt, and therefore automatically requires aggregates for the
collection logic, not a lot of fun :)

FB
--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #5
"Frans Bouma [C# MVP]" <pe************ ******@xs4all.n l> wrote in
news:xn******** *******@msnews. microsoft.com.. .
...
I can think of a lot of cases :) For example every I*able interface
describes behaviour. If you could implement an abstract implementation of
that interface which is written using for example the strategy pattern, you can create classes which should support a given behaviour very fast by
multiple inheriting from these abstract implementations , only filling in the blanks :)
I agree, abstract default-implementations of interfaces are nice, but why do
you need multiple inheritance for that?
Usually you have to implement an interface because some method of some other
class requires a reference to that interface - but you can implement that
interface anywhere, you don't have to create unneccessarey complex (and
hardly reusable) classes that implement two (or more) unrelated interfaces.

And, btw: C#'s event-keyword is far better than anything C++ has to offer
for "quickly filling in the blanks".
In .NET there is another example: a class which is a collection class and
which also should support COM+. It therefore has to inherit from
ServicedCompone nt, and therefore automatically requires aggregates for the
collection logic, not a lot of fun :)


I'm not sure if I got that example:
Using multiple inheritance, you would create one class derved from a
ServicedCompone nt and a collection class. Then you'd write an implementation
for every abstract method of ServicedCompone nt which calls a method
inherited from the collection class.
Ok, without MI, you do the same, but you call methods of a member instead of
an ancestor.
I don't see the difference?

Niki
Nov 16 '05 #6
Niki Estner wrote:
"Gaetan" <ga****@xeberon .net> wrote in
news:97******** *************** ***@posting.goo gle.com...
...
I know it s the same problem in java so there should be a way (a
receipe?) to avoid multiple inheritance.
Yes: It's called "software design".
If you need the functionality of two different classes, create two

instances of those classes and call methods these. If they need to communicate with each other to fullfil their job, create an interface to define what
communication may happen.
In 99.9% of the cases this approach will produce better code than using multiple inheritance.
If you think you have a case out of those other 0.1% - please post

it!

I have one - my GUI design:

There are two kinds of GUI controls - Basic controls decide what to do,
and theme controls decide how to do. Each UI *control* (seen by
end-user) actually consists of a basic control and a theme control.

For example, a Button may consist of a BasicButton and a
MacStyleButton. The "Button" itself is an interface. And the real class
(implementation ) created is MacStyleButton.

The MacStyleButton must inherit both of (abstract) BasicButton and
MacStyleControl . BasicButton can't just be an interface because it also
defines some common tasks, such as DoClick, Text (property), etc.

And for the theme, traditional theme engines decide only how controls
are rendered, so that they could use something like a ThemeEngine
class, providing RenderButton, RenderMenu, etc. But what if different
themes have different behaviors? For example, selecting a menu item in
windows and that in CDE: In windows you click menu, submenu, then
menuitem; but in CDE you press the button - and don't release, until
the mouse pointer moves to the item you want (submenus are expanded
automatically). In this case, a separated theme engine is not enough -
it must be tightly integrated into the core of GUI.

Besides, multiple-inheritance is not really error-prone - look at
Eiffel, Common Lisp Object System, Python, etc.
PS: Finally I gave up that GUI.

Nov 16 '05 #7
I think the cases where you need multiple inheritance are pretty rare, and
when you do need it, some kind of delegation / decorator pattern will
suffice. It's all too easy to choose multiple inheritance and end up with a
worse design, added complexity and often serious problems later on(diamonds
etc.).

In this example, a better design (IMO) would be to have the button "draw
into" a supplied object via a common interface. The supplied object is
plugged in and will represent the theme.

eg.
interface IThemePainter
{
void DrawBackground( rect,state);
void DrawInnerEdge(r ect,state);
void DrawText(text, rect,state);
...
}

In fact this is pretty much how it works in XP.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood
"Aquila Deus" <aq*********@ya hoo.co.uk> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
I have one - my GUI design:

There are two kinds of GUI controls - Basic controls decide what to do,
and theme controls decide how to do. Each UI *control* (seen by
end-user) actually consists of a basic control and a theme control.

For example, a Button may consist of a BasicButton and a
MacStyleButton. The "Button" itself is an interface. And the real class
(implementation ) created is MacStyleButton.

The MacStyleButton must inherit both of (abstract) BasicButton and
MacStyleControl . BasicButton can't just be an interface because it also
defines some common tasks, such as DoClick, Text (property), etc.

And for the theme, traditional theme engines decide only how controls
are rendered, so that they could use something like a ThemeEngine
class, providing RenderButton, RenderMenu, etc. But what if different
themes have different behaviors? For example, selecting a menu item in
windows and that in CDE: In windows you click menu, submenu, then
menuitem; but in CDE you press the button - and don't release, until
the mouse pointer moves to the item you want (submenus are expanded
automatically). In this case, a separated theme engine is not enough -
it must be tightly integrated into the core of GUI.

Besides, multiple-inheritance is not really error-prone - look at
Eiffel, Common Lisp Object System, Python, etc.
PS: Finally I gave up that GUI.

Nov 16 '05 #8
John Wood wrote:
I think the cases where you need multiple inheritance are pretty rare, and when you do need it, some kind of delegation / decorator pattern will suffice. It's all too easy to choose multiple inheritance and end up with a worse design, added complexity and often serious problems later on(diamonds etc.).
Yes, but conflicts can easily be solved by method/field renaming and
hiding.

After all, developers could still use interface (and they should do so
as much as possible).

In this example, a better design (IMO) would be to have the button "draw into" a supplied object via a common interface. The supplied object is plugged in and will represent the theme.

eg.
interface IThemePainter
{
void DrawBackground( rect,state);
void DrawInnerEdge(r ect,state);
void DrawText(text, rect,state);
...
}

In fact this is pretty much how it works in XP.
I also found that when I browsed the winform assembly (btw i think it's
really a dirty wrapper). But as I wrote, it's doesn't work well for
complex design. And the delegation, similiar this method, also requires
you to define what theme engine can do first, which limits the engine's
functionality.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood
"Aquila Deus" <aq*********@ya hoo.co.uk> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
I have one - my GUI design:

There are two kinds of GUI controls - Basic controls decide what to do, and theme controls decide how to do. Each UI *control* (seen by
end-user) actually consists of a basic control and a theme control.

For example, a Button may consist of a BasicButton and a
MacStyleButton. The "Button" itself is an interface. And the real class (implementation ) created is MacStyleButton.

The MacStyleButton must inherit both of (abstract) BasicButton and
MacStyleControl . BasicButton can't just be an interface because it also defines some common tasks, such as DoClick, Text (property), etc.

And for the theme, traditional theme engines decide only how controls are rendered, so that they could use something like a ThemeEngine
class, providing RenderButton, RenderMenu, etc. But what if different themes have different behaviors? For example, selecting a menu item in windows and that in CDE: In windows you click menu, submenu, then
menuitem; but in CDE you press the button - and don't release, until the mouse pointer moves to the item you want (submenus are expanded
automatically). In this case, a separated theme engine is not enough - it must be tightly integrated into the core of GUI.

Besides, multiple-inheritance is not really error-prone - look at
Eiffel, Common Lisp Object System, Python, etc.
PS: Finally I gave up that GUI.


Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
10082
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
22
23383
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
47
3640
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4930
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
15
28373
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting an error saying you are not allowed Multiple base classes. /// <summary>
7
3739
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
11
2249
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern to illustrate this. Problem: I need to expose two lists of objects from a high-level class. I would like to expose these lists as read-only, but require write access internally.
21
2481
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 { } class derived01 : baseclass01
2
2097
by: Immortal Nephi | last post by:
You may have heard diamond shape. You create one base class. One base class has member functions and member variables. You create two derived classes. All member functions and member variables from one base class are inherited into two derived classes. You want both derived classes to share member variables of the one base class. You can do this way so you don't need keyword -- friend. You can add virtual public One_Base_Class on...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.