General Development

Multiple inheritance == bad?? – CobraA1

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
OK, seeing this is the Christian coders network, I've decided to ask a question about coding: What's supposedly so bad about multiple inheritance? I know Java purposely doesn't have it, but I've always longed to know why.

I'm also wondering why it's so bad it has to be taken out of the language altogether - why are language makers making program design decisions for programmers??

------------------
There are two kinds of people: those who say to God, "Thy will be done," and those to whom God says, "All right, then, have it your way." -- C. S. Lewis

Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.

pillaroftruth

Member

Posts: 28
From: North Hollywood, CA, USA
Registered: 08-30-2003
i know what inheritence is... but what is multiple inheritance?
Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
Multiple inheritence isn't nessisarily bad.. and you can do some funky tricks with it
but it can create a mess..
and has its own issues

like what happens with methods, and properties that belong to both of the base classes?

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

BKewl

Member

Posts: 144
From: St. Charles, MO, USA
Registered: 07-10-2002
I think the main problem is with the dread "diamond of death:"

A
/ \
B C
\ /
\ /
D


(That will probably get messed up real bad, but it shows an inheritance hierarchy where a class D inherits from both B and C, and where B and C both inherit from a single class A).

This is basically what Klumsy said. Both B and C inherit from A, so the problematic part is figuring out whose version D gets. It's a pretty big mess. But multiple inheritance can be good sometimes, and even makes sense when you need it. But when you get down to it, you don't really need it that much at all.

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
inbreeding is just wrong, don't do it!
neither in your families, nor in your C++ class hyrachies

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
I suppose I can see where a programmer can go wrong, especially if classes B and C have the same methods. So, I guess I can see a point, but I still wish I could use multiple inheritance in Java.

For example, I have a class that I want to be part of an object hierarchy, and also be able to draw itself. At first glance, the more obvious solution is to inherit both from the object hierarchy and the drawing routines, but not possible without multiple inheritance. I ended up making a generalized object that inherited from the draw routines, and then another object that formed the hierarchy inherited from the generalized object.

In essence, I added another layer of complexity .

Sometimes, multiple inheritance is nice to have, despite possible failings. I really don't think the "diamond of death" is encountered very often - I usually want to use it when classes are very different, and have no common methods.

------------------
There are two kinds of people: those who say to God, "Thy will be done," and those to whom God says, "All right, then, have it your way." -- C. S. Lewis

Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
yeah MI really shines as a way to tack on different 'libraries' object of functionality to your classes.

how i would do your one, would be to have an Idraw interface that has the methods of drawing etc etc.
and each out your objects in the hyrachy would have to implement that IDraw
if you wanted that functionality to be in a seperate object, rather than implemented in each one of your classes, you make your 'draw'er' that also supports IDraw, then inside your class you create an instance of that class, "manually mapping " the Idraw methods and properties

well thats how i'd do it in C#, i think Java is similar enough in that case.

but that way you can have a hyrachy of whatever objects you want, some drawable some not.. you can take any object, and ask if it supports Idraw interface or not, can tell you which of these objects in the hyrachy are drawable.

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
does java, have the concept of INClUDe files?

if so you can simulate MI quite easily.. with have your IDraw interface
and then the standard code that implements those methods in an INCLUDE file, which gets included in every class, so you don't haveto copy paste, or implement the interface in every class and can update that code in one place..

C# 2.0 has a similar feature where a class be seperated into multiple files.. (where this shines for me) is currently there are autogenetared classes from XML datasets that get regenerated everytime the schema is changeds and makes sure there are properties etc for every field etc.. however i like to add methods and properties to this class (without having to decent from it).. having your class span multiple source files just rules, because only one of the files gets autorengerated.

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
Java does have an "import" statement, but it's not the same thing as including. Unlike C++, you cannot have functions or methods outside a class, and classes cannot be divided between several files.

It was just an example anyways. I'm not having problems with no multiple inheritance, I just sometimes wish I could use it.

Although Java supports only single inheritance, it does support multiple interfaces, which is nice, but not quite the same as multiple inheritance.

------------------
There are two kinds of people: those who say to God, "Thy will be done," and those to whom God says, "All right, then, have it your way." -- C. S. Lewis

Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.