I have no experience in C++, and I come from a Java background. Lately, I was asked in an interview on why Java would not allow multiple inheritence and the answer was pretty easy. However, I am still curious on how C++ deals with that since it allows you to inherit from more than one class.
Specifically, say there is a class called MechanicalEngineer
and another called ElectricalEngineer
. Both have a method called buildRobot()
.
What happens if we make a third class RoboticsEngineer
, that inherets from both and does not override that method, and you just call:
(some instance of RoboticsEngineer).buildRobot()
Will an exception be thrown, or the method from one of the super-classes will be used? If so, how does the compiler know which class to use?
So what is multiple inheritance? In multiple inheritance, a class inherits from two or more super classes. It inherits the methods and variables from all super classes. If you create an object, it has all methods and variables from the classes.
The only way to implement multiple inheritance is to implement multiple interfaces in a class. In java, one class can implements two or more interfaces. This also does not cause any ambiguity because all methods declared in interfaces are implemented in class.
In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B. But C# does not support multiple class inheritance.
The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.
The compiler will flag this kind of situation (i.e., attempting to call (some instance of RoboticsEngineer).buildRobot()
) as an error.
This happens because the derived object has got a copy of both base objects (a MechanicalEngineer
instance and an ElectricalEngineer
instance) inside of itself and the method signature alone is not enough to tell which one to use.
If you override buildRobot
in your RoboticsEngineer
, you will be able to say explicitly which inherited method to use by prefixing the class name, e.g.:
void RoboticsEngineer::buildRobot() { ElectricalEngineer::buildRobot() }
By the same coin, you can actually "force" the compiler to use one version or another of buildRobot
by prefixing it with the class name:
(some instance of RoboticsEngineer).ElectricalEngineer::buildRobot();
in this case the ElectricalEngineer
implementation of the method will be called, no ambiguity.
A special case is given when you have an Engineer
base class to both MechanicalEngineer
and ElectricalEngineer
and you specify the inheritance to be virtual
in both cases. When virtual
is used, the derived object does not contain two instances of Engineer
, but the compiler makes sure that there is only one of it. This would look like this:
class Engineer { void buildRobot(); }; class MechanicalEngineer: public virtual Engineer { }; class ElectricalEngineer: public virtual Engineer { };
In this case,
(some instance of RoboticsEngineer).buildRobot();
will be resolved without ambiguities. The same is true if buildRobot is declared virtual
and overridden in one of the two derived classes. Anyway, if both derived classes (ElectricalEngineer and MechanicalEngineer) overrides buildRobot
, then ambiguity arises once again and the compiler will flag the attempt at calling (some instance of RoboticsEngineer).buildRobot();
as an error.
It doesn't handle it. It's ambiguous. error C2385: ambiguous access of 'functionName'
The compiler is smart enough to know that it shouldn't guess your meaning.
For the program to compile, you need to tell the compiler that:
A. You know it's a problematic issue.
B. Tell it what exactly you mean.
To do this, you need to explicitly tell the compiler which method you're asking for:
RoboticsEngineer myRobot; myRobot.ElectricalEngineer::buildRobot();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With