What are the problem with multiple inheritence ?
Multiple inheritence is commnon in the real word, so it is also useful in modeling real - world enities in programming.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akkiraju IvaturiPosted Aug 24, 2012, 10:06 PM
The most obvious problem is with function overriding.
Let's say have two classes A and B, both of which define a method "doSomething". Now you define a third class C, which inherits from both A and B, but you don't override the "doSomething" method.
When the compiler seed this code...
...which implementation of the method should it use? Without any further clarification, it's impossible for the compiler to resolve the ambiguity.
Besides overriding, the other big problem with multiple inheritance is the layout of the physical objects in memory.
Languages like C++ and Java and C# create a fixed address-based layout for each type of object. Something like this:
When the compiler generates machine code (or bytecode), it uses those numeric offsets to access each method or field.
Multiple inheritance makes it very tricky.
If class C inherits from both A and B, the compiler has to decide whether to layout the data in AB order or in BA order.
But now imagine that you're calling methods on a B object. Is it really just a B? Or is it actually a C object being called polymorphically, through its B interface? Depending on the actual identity of the object, the physical layout will be different, and its impossible to know the offset of the function to invoke at the call-site.
The way to handle this kind of system is to ditch the fixed-layout approach, allowing each object to be queried for its layout before attempting to invoke the functions or access its fields.
So...long story short...it's a pain in the neck for compiler authors to support multiple inheritance. So when someone like Guido van Rossum designs python, or when Anders Hejlsberg designs c#, they know that supporting multiple inheritance is going to make the compiler implementations significantly more complex, and presumably they don't think the benefit is worth the cost.