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?? ------------------ Switch Mayhem now available! Get it here |
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? ------------------ |
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
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 ------------------ |
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. ------------------ Switch Mayhem now available! Get it here |
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. 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. ------------------ |
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 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. ------------------ |
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. ------------------ Switch Mayhem now available! Get it here |