Multiple inheritance in Java/C#

H

Hung Jung Lu

Hi,

I think Microsoft did look into Python when they designed C#. (E.g.
they got rid of checked exceptions of Java.) However, they followed
Java in avoiding multiple inheritance (MI), which is a great leap
backward from C++, in my opinion. I understand the why of avoiding
virtual data members, since performance is an issue. I understand the
problems of MI in C++. But the cure (using interfaces) seems worse
than the disease itself. Now if you change an interface, you have to
go and change all classes that use the interface. For Java, this is
discussed in:

http://www.cyberdyne-object-sys.com/oofaq2/DynInh.htm

Standard reply is a cold sholder: "interfaces are not meant to be
changed". But we live in the real world. Also, I don't think it is fun
to write zillions of getters/setters, and wrappers for implementing
interface methods in MI of interfaces. Sure, you can have the IDE
writing some of these codes, but that brings other sets of problems,
too.

Python of course is free from these problems. To start with, Python
data members are virtual, easily overridable. I remember people used
to complain that Python does not support encapsulation and that
private data in inheritance chain could be overriden. But that
actually is GOOD in many cases, and especially in MI a la MixIn.

Does anyone know how to emulate Python-like MI in Java/C#?

regards,

Hung Jung
 
T

Thomas

I think Microsoft did look into Python when they designed C#.

I had the same feeling.

However, they followed Java in avoiding multiple inheritance (MI),
which is a great leap backward from C++, in my opinion.

There are pros and cons for MI; I never liked the MI design (e.g.
because it can lead to nasty bugs, which are really hard to find)
and prefer C#'s way.

But the cure (using interfaces) seems worse than the disease
itself. Now if you change an interface, you have to go and
change all classes that use the interface.

Standard reply is a cold sholder: "interfaces are not meant
to be changed". But we live in the real world.

At least, the compiler tells you exactly what to do; in my
experience, this leads to very stable applications, even if the
application gets very large. (If you work with MI, it might happen
as well that you have to change many classes when you make changes
to the base classes.)

I hope that C# uses it's chance of being a (IMHO) well designed
language, which can concentrate now on performance and stability
issues (and not on implementing new features). Python used to be
that way, but in the last time, it is developing too fast (IMHO).
 
S

Sean Ross

Hung Jung Lu said:
Does anyone know how to emulate Python-like MI in Java/C#?

Hi.
I believe this is usually done using a design pattern - I think its called
"the proxy object pattern" or it may be "the delegator pattern", I'm not
sure. I know that for doing CORBA programming with Java, you can have tie
classes (which I believe use the aforementioned design pattern) generated to
help simulate multiple inheritance. The idea is something like this: you
have class C which inherits from A but you would like to have a class that
inherits from A and B; make a class D that inherits from B, and that keeps
an instance of C; D is your class that "inherits" from A and B; when you
have an instance of d and you call a method derived from A, you delegate to
the instance of C, otherwise you handle it directly. I think that's the
general idea. You should be able to find specific examples somewhere online.

HTH
Sean
 
M

Michele Simionato

Hi,

I think Microsoft did look into Python when they designed C#. (E.g.
they got rid of checked exceptions of Java.) However, they followed
Java in avoiding multiple inheritance (MI), which is a great leap
backward from C++, in my opinion. I understand the why of avoiding
virtual data members, since performance is an issue. I understand the
problems of MI in C++. But the cure (using interfaces) seems worse
than the disease itself. Now if you change an interface, you have to
go and change all classes that use the interface. For Java, this is
discussed in:

http://www.cyberdyne-object-sys.com/oofaq2/DynInh.htm

Standard reply is a cold sholder: "interfaces are not meant to be
changed". But we live in the real world. Also, I don't think it is fun
to write zillions of getters/setters, and wrappers for implementing
interface methods in MI of interfaces. Sure, you can have the IDE
writing some of these codes, but that brings other sets of problems,
too.

Python of course is free from these problems. To start with, Python
data members are virtual, easily overridable. I remember people used
to complain that Python does not support encapsulation and that
private data in inheritance chain could be overriden. But that
actually is GOOD in many cases, and especially in MI a la MixIn.

Does anyone know how to emulate Python-like MI in Java/C#?

regards,

Hung Jung

Yeah, I am not convinced at all by the interfaces strategy; if not
real multiple inheritance, they could at least have provided something
along the way of Ruby's mixins.

About implementing Python MI in Java/C#: in principle you could do it
yourself, by following the MRO algorithm described in

http://www.python.org/2.3/mro.html

In practice, I do expect to implement MI properly to be cumbersome
and with a (possibly high) performance price.

A good idea is to ask Jython developers.


Michele Simionato
 
H

Hung Jung Lu

Sean Ross said:
Hi.
I believe this is usually done using a design pattern - I think its called
"the proxy object pattern" or it may be "the delegator pattern", I'm not
sure. I know that for doing CORBA programming with Java, you can have tie
classes (which I believe use the aforementioned design pattern) generated to
help simulate multiple inheritance. The idea is something like this: you
have class C which inherits from A but you would like to have a class that
inherits from A and B; make a class D that inherits from B, and that keeps
an instance of C; D is your class that "inherits" from A and B; when you
have an instance of d and you call a method derived from A, you delegate to
the instance of C, otherwise you handle it directly. I think that's the
general idea. You should be able to find specific examples somewhere online.

Thanks. I am finding it to be the standard approach. In short:

(containment+delegation) ---> aggregation.

Kind of laborious. But that's the way of life without true MI.

Hung Jung
 
H

Hung Jung Lu

Thomas said:
There are pros and cons for MI; I never liked the MI design (e.g.
because it can lead to nasty bugs, which are really hard to find)
and prefer C#'s way.

If you use MI to inherit classes and do cascade calls (calling parent
methods with same name, like the case of constructors) up the tree of
inheritance, sure, you get into big mess. But if you use MI as MixIns
without cascade calls, you won't have those nasty bugs. I don't think
I like C# or Java for their lack of MI. You see pairs of
interfaces/implementations all over places, and use
containment+delegation to emulate MI. Things that can be done in a few
lines of code in C++, will be typically multiplied by a factor about 3
when you do them in Java/C#. More over, if you change your interface,
say, just adding one more method, you'll have to make changes all over
places.
At least, the compiler tells you exactly what to do; in my
experience, this leads to very stable applications, even if the
application gets very large. (If you work with MI, it might happen
as well that you have to change many classes when you make changes
to the base classes.)

I think we have different purposes for MI. I use MI for MixIns, not as
real classes, and no cascade calls upstream. The value of MixIns to me
is in their code/action, not in their object/data. I can understand
difficulties when you use MI for object/data. In C++ I can add a new
method in a great-grand-parent class in line, and touch the code only
in two spots: where it is defined, and where it is used. In Java/C#,
it would be hell.
I hope that C# uses it's chance of being a (IMHO) well designed
language, which can concentrate now on performance and stability
issues (and not on implementing new features). Python used to be
that way, but in the last time, it is developing too fast (IMHO).

I can't speak on how well designed it is. But I do know that both Java
and C# are now adding generic templates. So at least the original
designs were not complete.

Hung Jung
 
D

Dieter Maurer

Sean Ross said:
"the delegator pattern", I'm not
sure. I know that for doing CORBA programming with Java, you can have tie
classes (which I believe use the aforementioned design pattern) generated to
help simulate multiple inheritance. The idea is something like this: you
have class C which inherits from A but you would like to have a class that
inherits from A and B; make a class D that inherits from B, and that keeps
an instance of C; D is your class that "inherits" from A and B; when you
have an instance of d and you call a method derived from A, you delegate to
the instance of C, otherwise you handle it directly. I think that's the
general idea. You should be able to find specific examples somewhere online.

That is indeed the general approach.
And, as you see, you write lots of trivial delegation code.
Trivialities that the compiler would do for you -- had you a
language with multiple inheritance.


Dieter
 
D

Dieter Maurer

... lacking multiple inheritance ...
At least, the compiler tells you exactly what to do; in my
experience, this leads to very stable applications, even if the
application gets very large. (If you work with MI, it might happen
as well that you have to change many classes when you make changes
to the base classes.)

I am a fan of inheritance. Only multiple inheritance provides
mix in classes that let you construct your class out of
components: you need persistence, add a "Persistence" mixin class;
you need properties, add a "PropertyManager" class;
you need indexing, add an "Indexible" class....

I build complex systems (multi-media database, E-Commerce platform,
content management system)
making extensive use of multiple inheritance
(C++ and Python). I think, I do it extremely fast and efficient --
both for the initial implementation as well as for maintenance.
Much more efficient than in a language without multiple inheritance
(I have Java experience, as well).

It is *very* rare in practice that a base class change affects derived classes,
although it is easy to construct examples for this case.
I hope that C# uses it's chance of being a (IMHO) well designed
language, which can concentrate now on performance and stability
issues (and not on implementing new features). Python used to be
that way, but in the last time, it is developing too fast (IMHO).

It would only be too fast, when it would introduce a high rate of
errors. Nobody hinders you to stay behind...


Dieter
 
D

Duncan Booth

In C# 2.0 it looks like they copied Python's generator functions:

http://dev.r.tucows.com/extdocs/c_sharp_2.0_spec.html#_Toc48187963

That looks fun. I see they've also picked up on lambda functions and nested
scope variables. I especially liked this:
static void Main() {
foreach (D d in F()) d();
} ....
static D[] F() {
D[] result = new D[3];
int x = 0;
for (int i = 0; i < 3; i++) {
int y = 0;
result = delegate { Console.WriteLine("{0} {1}", ++x, ++y); };
}
return result;
}

the three delegates capture the same instance of x but separate
instances of y, and the output is:

1 1
2 1
3 1
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top