[OT] Java, C#: why no multiple inheritance?

C

crichmon

Hi,

The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance? I am aware that they do allow
classes to implement multiple Interfaces in addition to the optional single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design? I have typically found that multiple inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info out
there?

thanks,
crichmon
 
P

Phlip

crichmon said:
The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance?

The designers of Java found it difficult to syntactically specify, and
difficult to implement, so they instead decided that "multiple inheritance
of implementation is bad, like goto, so we will forbid our programmers doing
bad things".
I am aware that they do allow
classes to implement multiple Interfaces in addition to the optional single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design?

C# went along with this because it's Java with the names of a few keywords
changed, and a few minor features added.
I have typically found that multiple inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info out
there?

Do not deny me a technique just because others have abused it. Multiple
inheritance of implementation is occassionally the cleanest alternative.

None of these languages support delegation very well. That's often cleaner.
 
M

Markus Dehmann

The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance? I am aware that they do
allow
classes to implement multiple Interfaces in addition to the optional
single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design? I have typically found that multiple
inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info
out
there?

Multiple inheritance of implementation has the "diamond problem", see also:
FAQ [25.8] What is the "dreaded diamond"?

Imagine you have a base class Animal with a talk() member function, and
two derived classes, Frog and Dinosaur who both have their own way to
talk(). Then, derive a Frogosaur class from Frog, Dinosaur.

class Frogosaur: public Frog, public Dinosaur{
};

Which inherited talk() method should be called when your Frogosaur speaks?
It's not clear. There are two implementations of talk() available in your
ugly Frogosaur. Poor schizophrenic thing! The same applies to variables,
of course. The frog's member variable int size is 3, and the dinosaur's
int size is 1000. What's the Frogosaur's size? 1000 and 3?

That's why Java does not allow this.
 
M

Markus Dehmann

The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance? I am aware that they do
allow
classes to implement multiple Interfaces in addition to the optional
single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design? I have typically found that multiple
inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info
out
there?

Not about multiple inheritance, but about inheriting interfaces vs
inheriting implementations in general:
Scott Meyers, More Effective C++:
Item 33. Make Non-Leaf Classes Abstract.

He explains there that it is generally a good idea not to derive concrete
classes (implementations) from one another at all.
 
P

Phlip

Markus said:
Multiple inheritance of implementation has the "diamond problem", see also:
FAQ [25.8] What is the "dreaded diamond"?

Imagine you have a base class Animal with a talk() member function, and
two derived classes, Frog and Dinosaur who both have their own way to
talk(). Then, derive a Frogosaur class from Frog, Dinosaur.

class Frogosaur: public Frog, public Dinosaur{
};

Which inherited talk() method should be called when your Frogosaur speaks?
It's not clear. There are two implementations of talk() available in your
ugly Frogosaur. Poor schizophrenic thing! The same applies to variables,
of course. The frog's member variable int size is 3, and the dinosaur's
int size is 1000. What's the Frogosaur's size? 1000 and 3?

That's why Java does not allow this.

Deferring to the class listed first in the base class list is a solution
well within the Java way of things.

I think Java did it because they hard-coded the vtable concept into their
VM, but wrote it in a way that prevented deferrment.

(Nice "Frogosaur" example, though!)
 
M

Markus Dehmann

Markus said:
Multiple inheritance of implementation has the "diamond problem", see also:
FAQ [25.8] What is the "dreaded diamond"?

Imagine you have a base class Animal with a talk() member function, and
two derived classes, Frog and Dinosaur who both have their own way to
talk(). Then, derive a Frogosaur class from Frog, Dinosaur.

class Frogosaur: public Frog, public Dinosaur{
};

Which inherited talk() method should be called when your Frogosaur
speaks?
It's not clear. There are two implementations of talk() available in
your
ugly Frogosaur. Poor schizophrenic thing! The same applies to variables,
of course. The frog's member variable int size is 3, and the dinosaur's
int size is 1000. What's the Frogosaur's size? 1000 and 3?

That's why Java does not allow this.

Deferring to the class listed first in the base class list is a solution
well within the Java way of things.

That's the way Perl handles it
(http://www.fact-index.com/m/mu/multiple_inheritance.html)
Maybe that sounded already too "hackish" for the Java designers.

But yes, probably there are more reasons why Java doesn't allow it.
(Nice "Frogosaur" example, though!)

Unfortunately not from me ;) (google "frogosaur")
 
D

David Hilsee

Deferring to the class listed first in the base class list is a solution
well within the Java way of things.

Perhaps the Java languge designers decided that it was confusing to do it
that way. IMHO, that seems like an arbitrary rule that might be hard to
remember. Besides, that doesn't begin to address the data initialization
problem with virtual base classes, etc. I think the Java language doesn't
contain multiple inheritance simply because there were too many rules that
had to be made. Language simplicity (unless it was for the sake of
portability, I suppose) seemed to be one of Java's primary design goals.
 
C

crichmon

Markus Dehmann said:
The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance? I am aware that they do
allow
classes to implement multiple Interfaces in addition to the optional
single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design? I have typically found that multiple
inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info
out
there?

Multiple inheritance of implementation has the "diamond problem", see also:
FAQ [25.8] What is the "dreaded diamond"?

I understand the dreaded diamond... and while it is a possibility with
multiple inheritance, just because a program makes use of multiple
inheritance doesn't mean that it will definitely involve the dreaded
diamond.

Imagine you have a base class Animal with a talk() member function, and
two derived classes, Frog and Dinosaur who both have their own way to
talk(). Then, derive a Frogosaur class from Frog, Dinosaur.

class Frogosaur: public Frog, public Dinosaur{
};

Which inherited talk() method should be called when your Frogosaur speaks?
It's not clear. There are two implementations of talk() available in your
ugly Frogosaur. Poor schizophrenic thing!

This can be solved by overriding talk() with a new version for Frogosaur.

The same applies to variables,
of course. The frog's member variable int size is 3, and the dinosaur's
int size is 1000. What's the Frogosaur's size? 1000 and 3?

If Frog and Dinosaur each implement size, rather then inheriting it from
Animal, then you're speaking of a problem that exists beyond the diamond.
If that situation were to arrise, then I would say that Frog and Dinosaur
should inherit size from Animal (and inherit Animal virtually as well) which
is a diamond... but in that case the '1000 or 3' size problem is then
irrelevant as the Frogosaur can set the single size variable
virtually-inherited from Animal to whatever is appropriate for a Frogosaur.

With proper coding, the only schizophrenia I see existing is that of a
developer who may get confused by such possibilities.

That's why Java does not allow this.

Bad usage of multiple inheritance can lead to ugly situations yes, but bad
usage of other language possibilities can also produce ugly situations.
(Ever see a function try to return a local variable as a reference?)

Simply using it doesn't mean that one will definitely have ugly
situations...


crichmon
 
C

crichmon

Phlip said:
The designers of Java found it difficult to
syntactically specify, and difficult to implement,
so they instead decided that "multiple inheritance
of implementation is bad, like goto, so we will
forbid our programmers doing bad things".
Erg...



C# went along with this because it's Java with the
names of a few keywords changed, and a few minor
features added.

Thnx for the info!

Do not deny me a technique just because others
have abused it.
Amen!


Multiple inheritance of implementation is
occassionally the cleanest alternative.
None of these languages support delegation very
well. That's often cleaner.

crichmon
 
M

Markus Dehmann

Markus Dehmann said:
The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance? I am aware that they do
allow
classes to implement multiple Interfaces in addition to the optional
single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design? I have typically found that multiple
inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info
out
there?

Multiple inheritance of implementation has the "diamond problem", see also:
FAQ [25.8] What is the "dreaded diamond"?

I understand the dreaded diamond... and while it is a possibility with
multiple inheritance, just because a program makes use of multiple
inheritance doesn't mean that it will definitely involve the dreaded
diamond.

Imagine you have a base class Animal with a talk() member function, and
two derived classes, Frog and Dinosaur who both have their own way to
talk(). Then, derive a Frogosaur class from Frog, Dinosaur.

class Frogosaur: public Frog, public Dinosaur{
};

Which inherited talk() method should be called when your Frogosaur
speaks?
It's not clear. There are two implementations of talk() available in
your
ugly Frogosaur. Poor schizophrenic thing!

This can be solved by overriding talk() with a new version for Frogosaur.

The same applies to variables,
of course. The frog's member variable int size is 3, and the dinosaur's
int size is 1000. What's the Frogosaur's size? 1000 and 3?

If Frog and Dinosaur each implement size, rather then inheriting it from
Animal, then you're speaking of a problem that exists beyond the diamond.
If that situation were to arrise, then I would say that Frog and Dinosaur
should inherit size from Animal (and inherit Animal virtually as well)
which
is a diamond... but in that case the '1000 or 3' size problem is then
irrelevant as the Frogosaur can set the single size variable
virtually-inherited from Animal to whatever is appropriate for a
Frogosaur.

With proper coding, the only schizophrenia I see existing is that of a
developer who may get confused by such possibilities.

That's why Java does not allow this.

Bad usage of multiple inheritance can lead to ugly situations yes, but
bad
usage of other language possibilities can also produce ugly situations.
(Ever see a function try to return a local variable as a reference?)

That's fine in Java. :) (with g++ you get a warning)
Simply using it doesn't mean that one will definitely have ugly
situations...

.... which leads directly to the different language design philosophies
between Java and C++ (which have been discussed a 10,000 times here and
elsewhere, so let's make it short): Java is a clean, safe and simple
language, C++ is a flexible, fast, unsafe and complex language. If
something might lead to bad design, unreadable code, undefined behavior,
leaks or errors, Java excludes it (even if it makes the language less
flexible), C++ trusts you and lets you do it.

So, if you say: It CAN be ugly, but it doesn't HAVE to -- well, then that
means: A language like C++ will allow it, but a language like Java will
disallow it.

C++ allows the dreaded diamond, maybe because the problem was not known at
design time (C++'s long history shines through everywhere), or simply
because it wants to restrict as less as possible. Hey, C++ even has
exceptions AND dumb pointers, which in conjunction almost surely lead to
memory leaks (but again, doesn't HAVE to if you are very, very careful).

Java is like a clean, rich, bright and friendly welfare state, like
Sweden. C++ is like a dangerous jungle where you are on your own. And in
the jungle you might even meet a Frogosaur. Or a young dynamic_cast next
to a very (old)cast.

Markus
 
P

Peter Ammon

crichmon said:
Hi,

The main language that I progam in is C++. I have been studying and
experimenting with Java and C# and I'm somewhat curious as to why those
languages don't allow multiple inheritance? I am aware that they do allow
classes to implement multiple Interfaces in addition to the optional single
inheritance from another class, but I'm concerned at the fact that
Interfaces don't allow for data members, just member functions. Is the
inheritance thing to prevent code bloat and/or preserve some ideal of
object-oriented design? I have typically found that multiple inheritance in
C++ can be very advantageous, and I'm concerned as to how I might have to
re-design my objects if I ever convert C++ code to C# or Java. Any info out
there?

thanks,
crichmon

I'd say because it's very hard to implement and very very hard to
understand fully, so much that it may not be worth understanding fully.
The articles about it are mind bending; see for example
<http://www.webcom.com/haahr/dylan/linearization-oopsla96.html> on
multiple inheritance in Dylan. For a language that prides itself on
transparency like Java, forcing people to understand "linearizations"
and the like just to figure out which method gets executed is probably
not a good idea.

That said, if you feel you understand it fully enough to use it safely,
more power to you. Enjoy a language that supports it. You have many
options: Eiffel, Dylan, Perl, LISP + CLOS, C++... I admit to wishing
for it more than a few times in Objective-C.
 
J

jeffc

crichmon said:
I understand the dreaded diamond... and while it is a possibility with
multiple inheritance, just because a program makes use of multiple
inheritance doesn't mean that it will definitely involve the dreaded
diamond.

That's not why Java doesn't allow it - at least not directly. You could say
that big cars use a lot of gas, and that's why small sports cars don't allow
back seats. Or you could say that small back seats don't have much leg room
and that's why large cars don't allow small back seats. (Don't get carried
away anyone - I'm not comparing one language to a sports car or anything.)
The point is, it's simply an overall design philosophy, not a technical
jigaboo.
 
J

jeffc

jeffc said:
The point is, it's simply an overall design philosophy, not a technical
jigaboo.

Just came to my attention that this word means something to some people that
I wasn't aware of - just my ignorance, no offense intended. I suppose I was
thinking more along the lines of "bugaboo".
 
J

John Owens

Simply change the relationship from a "is a" to a "has a", I know this might
result in making some functions public (or at least creating a friend
function to read these variables) but it is a lot simpler and that allows it
to grow easilier - It's not as cool though :).

Generally I try to be very careful now about when I use inheritance, I think
it is overused by a lot of programs (including me until recently).
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top