1 2 3 Previous Next 35 Replies Latest reply on Jan 5, 2005 10:01 AM by jwenting

    Multiple Inheritance

      Why is Multiple implementation inheritence not allowed in Java.
      Do we have a logic to put forward ?
        • 1. Re: Multiple Inheritance
          Why is Multiple implementation inheritence not allowed
          in Java.
          Do we have a logic to put forward ?
          I think it's to keep things simple. You can achieve a similar functionallity implementing multiple interfaces.
          • 2. Re: Multiple Inheritance
            You can do multiple inheritance of interfaces, just not concrete classes. - MOD
            • 3. Re: Multiple Inheritance
              But Why ?????
              • 4. Re: Multiple Inheritance
                Again, the answer is simplicity. Have you ever seen virtual base classes in C++? Yech!!! Much of what is achieved with multiple inheritance in C++ can be done with single class inheritance + multiple interface implementation in Java. Why don't you put forward an example which you thing requires multiple class inheritance and see if a posted can give an alternate solution.
                • 5. Re: Multiple Inheritance
                  It's because of problems with diamond inheritance described for example in The Java Programming language by Arnold/Gosling.
                  • 6. Re: Multiple Inheritance
                    Don't have the book, can u please quote some of them.
                    • 7. Re: Multiple Inheritance
                      Don't have the book, can u please quote some of them.
                      • 8. Re: Multiple Inheritance
                        But you do have google don't you?

                        • 9. Re: Multiple Inheritance
                          Because of the "multiple inheritance" double diamond.

                          Let's say you have two concrete C++ classes named Cowboy and Artist, both with methods named draw(). If you have another class that extends both Cowboy and Artist, what happens when your class calls the draw() method? Do you get a nice oil painting or a rustler dead in the street?

                          You might answer that this is a silly example, and the obvious solution is that Cowboy and Artist simply need to be made orthogonal. But it's not always possible to make classes orthogonal, and there's nothing in C++ to prohibit this situation.

                          So rather than deal with it, Jim Gosling decided to allow only single inheritance for extension and multiple inheritance for interfaces. You're forced to keep interfaces orthogonal.

                          If you have Cowboy and Artist interfaces in Java, and both have a public method draw() that returns void, you'll have to sort out the implementation for yourself when you write your concrete class that implements both. The burden is on you, not the compiler. It doesn't have to make a decision anymore.

                          That's my understanding of it. - MOD
                          • 10. Re: Multiple Inheritance
                            Thanks duffymo. Good One.
                            • 11. Re: Multiple Inheritance
                              So rather than deal with it, Jim Gosling decided to
                              Poor Jim. He always gets the blame when James has been bad. -:)
                              • 12. Re: Multiple Inheritance
                                Does he prefer "James"? We're not on a first-name basis. ;)
                                • 13. Re: Multiple Inheritance
                                  I have an example where mutiple inheritance would be very handy.
                                  |                              -> BaseMySqlObject
                                  |        ObjectI -> BaseObject -> BaseOracleObject
                                  |                              -> BaseDB2Object
                                  |                      |
                                  |                      |
                                  |                      |          
                                  |                      V            -> ExtendedMySqlObject
                                  |ExtendedObjectI -> ExtendedObject  -> ExtendedOracleObject
                                  |                                   -> ExtendedDB2Object
                                  BaseObject defines all data and data methods a() through m();
                                  Base[DB]Object defines fetch() which populates all data extended from BaseObject.

                                  ExtendedObject extends BaseObject defines extra data and methods n() - z();
                                  Extended[DB]Object extends ExtendedObject and defines insert() which does a DB specific function.

                                  The problem is the Extended[DB]Object should also reproduce the functionality of Base[DB]Object. Without multiple inheritance I need to either copy code from the ExtendedObject or Base[DB]Object into each of the Extended[DB]Objects.

                                  ExtendedMySqlObject can extend ExtendedObject or BaseMySqlObject but not both. I understand that the suggested solution would be to use interfaces, but the issue is with having to reproduce redundant functional code.
                                  I can make ExtendedObject an interface but the n() - z() methods will have to be implemented in each of the Extended[DB]Objects. This also means I will have to maintain all copies to remain in sync. This is not code reuse...

                                  The only work around I have for this is to use delegation and instantiate internal objects, but this still results in redundant code and I lose direct access to the internal data...

                                  ie. If I extend the Base[DB]Object classes and inherit ExtendedObjectI then n()-z() will still need to exist in all of the Extended[DB]Objects, but it will be simply relaying the calls to an internal ExtendedObject

                                  n() {

                                  This is a messy workaround and could be avoided if I could simply extend both classes.

                                  I understand the downfall as well. If ExtendedObject and BaseDB2Object both
                                  override BaseObject.a() then which would ExtendedDB2Object use. But I think this would be a rare case and certain rules could be defined to indicate which to use as the primary from the ExtendedDB2Object class.

                                  This might seem convoluted but if you understand what I'm getting at maybe you can provide me with a clean way to achieve what I want without 50% extra code used to get around the lack multiple inheritance. If not then maybe there is use for multiple inheritance after all.
                                  • 14. Re: Multiple Inheritance
                                    Why don't you use extends?
                                    1 2 3 Previous Next