On a theory level a class can inherit more than one class.
Such situations where a class inherits two or more classes is called multiple inheritance.
As multiple inheritance is in practice quite a complex mechanism a good starting point to it is to aim to avoid multiple inheritance all together.
At the same time it has a few very clear and sensible uses.
In multiple inheritance a class inherits the features of its base classes meaning multiple inheritance is inheritance.
A subclass object consists of its base class objects as well as the base class’ own part augmenting the functiontality of the base class parts.
Polymorphism applies also in multiple inheritance: a subclass object is valid as any of its base class objects.
It is very important to remember when speaking about multiple inheritance: a subclass object is always an object of (all of) its base class(es).
If there is no is-a relationship to all base classes the correct solution instead of inheritance is for example composition i.e. the objects of the other classes are set as member variables.
If needed the member functions of the member variables can be called also with composition through a so called
call-through functions, which only call the member function of the member variable.
The most obvious use for multiple inheritance is implementing the interfaces provided by interface functions.
Several interfaces can be added into a class by multiple inheritance and then implement them in the sub class.
Another use for multiple inheritance is combining two or more classes into a class that extends and augments several existing classes.
The most common use for this is object libraries or frameworks that provide classes that have been designed to be used exactly in these kinds of situations.
The risk is, however, that the base classes disturb eachother’s behaviour.
For example, it is possible to inherit the same member function from both of the base classes.
Another possible problem is repeated multiple inheritance where the same baseclass is inherited into the subclass indirectly more than once.
Even though this may in some situation be a desirable outcome, there are also several dangers included as well. At this point it is best to just avoid repeated multiple inheritance.
Multiple inheritance must not be used if the subclass has no clear is-a inheritance relationship to both of its base classes.
It cannot be used so that the subclass would be a representative of one of its base classes part of the time and part of the time the others: it is always both.
Multiple inheritance isn’t a way to get some functionality from some class as a part of the subclass as the entire public interface of the base class is always inherited.
Multiple inheritance also makes the structure of the program more complex and it should therefore be used with caution.
Often for example a member variable is the correct solution.
Java and C++ form an interesting language pair when it comes to multiple inheritance: In Java multiple inheritance does not exist on the class level and each class can have exactly one base class.
Nonetheless, Java does not fully forbid multiple inheritance. It is limited to implementing interfaces
(ìnterface) meaning Java allows multiple inheritance of interface classes.
C++ in turn leaves everything to the responsibility of the programmer: multiple inheritance can be used.
This gives the programmer awesome opportunities but also leaves all danger zones up to the programmer to avoid.
Multiple inheritance is also in use fully on class level in C++ also for interface classes.
C++ has no separate syntax for interface classes like Java does. Interface class is just a class with all member functions virtual and no implementation provided to any of them in the base class.