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

Discussion in 'C++' started by crichmon, Jul 9, 2004.

  1. crichmon

    crichmon Guest

    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
     
    crichmon, Jul 9, 2004
    #1
    1. Advertising

  2. crichmon

    Phlip Guest

    crichmon wrote:

    > 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.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 9, 2004
    #2
    1. Advertising

  3. On Thu, 08 Jul 2004 23:14:58 GMT, crichmon <> wrote:

    > 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.
     
    Markus Dehmann, Jul 9, 2004
    #3
  4. On Thu, 08 Jul 2004 23:14:58 GMT, crichmon <> wrote:

    > 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.
     
    Markus Dehmann, Jul 9, 2004
    #4
  5. crichmon

    Phlip Guest

    Markus Dehmann wrote:

    > 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!)

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 9, 2004
    #5
  6. On Fri, 09 Jul 2004 03:12:42 GMT, Phlip <> wrote:

    > Markus Dehmann wrote:
    >
    >> 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")
     
    Markus Dehmann, Jul 9, 2004
    #6
  7. crichmon

    David Hilsee Guest

    > 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.

    --
    David Hilsee
     
    David Hilsee, Jul 9, 2004
    #7
  8. crichmon

    crichmon Guest

    "Markus Dehmann" <> wrote in message
    news:eek:...
    > On Thu, 08 Jul 2004 23:14:58 GMT, crichmon <> wrote:
    >
    > > 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
     
    crichmon, Jul 9, 2004
    #8
  9. crichmon

    crichmon Guest

    "Phlip" <> wrote in message
    news:_glHc.2786$m%...
    > crichmon wrote:
    >
    >> 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".


    Erg...


    >> 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.


    Thnx for the info!


    >> 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.


    Amen!


    > Multiple inheritance of implementation is
    > occassionally the cleanest alternative.


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


    crichmon
     
    crichmon, Jul 9, 2004
    #9
  10. On Fri, 09 Jul 2004 05:42:26 GMT, crichmon <> wrote:

    >
    > "Markus Dehmann" <> wrote in message
    > news:eek:...
    >> On Thu, 08 Jul 2004 23:14:58 GMT, crichmon <>
    >> wrote:
    >>
    >> > 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
     
    Markus Dehmann, Jul 9, 2004
    #10
  11. crichmon

    Peter Ammon Guest

    crichmon wrote:

    > 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.

    --
    Pull out a splinter to reply.
     
    Peter Ammon, Jul 9, 2004
    #11
  12. crichmon

    jeffc Guest

    "crichmon" <> wrote in message
    news:6fqHc.7829$...
    >
    > 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.
     
    jeffc, Jul 9, 2004
    #12
  13. crichmon

    jeffc Guest

    "jeffc" <> wrote in message
    news:...
    > 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".
     
    jeffc, Jul 9, 2004
    #13
  14. crichmon

    John Owens Guest

    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).

    "crichmon" <> wrote in message
    news:SzkHc.7380$...
    > 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
    >
    >
     
    John Owens, Jan 19, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,199
    Martijn van Steenbergen
    Dec 21, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    989
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,251
    Smokey Grindel
    Dec 2, 2006
  4. Sreenivas
    Replies:
    18
    Views:
    928
    Wildemar Wildenburger
    Jan 17, 2008
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    627
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page