Multiple inheritance


Multiple inheritance allows a class to have more than one base class. For example a particular kind of car may be a jeep and a family car at the same time as illustrated in Figure 17.5. The FamilyCarJeep is thus derived from the FamilyCar as well as the Jeep.

Figure 17.5. An example of multiple inheritance

Even though multiple inheritance is a powerful concept, which is supported by languages like C++ and Eiffel it also involves several subtleties that can make its implementation tricky.



Multiple inheritance is a heavily debated concept in the programming community where pros and cons are thrown back and forth between its supporters and detractors.

One of the problems often mentioned by the detractors includes a scenario where the two base classes both contain a method with the same signature but different implementations. Which of the two versions should the derived class then inherit? For example, suppose both the FamilyCar and the Jeep classes implement the method ToString. The FamilyCar version returns the values of the brandName and odometer instance variables (in a string) whereas the Jeep returns the numberOfSeats and the suspensionSystemName. If the FamilyCarJeep does not override the ToString method, which ToString version should it inherit?

A similar problem is encountered when the two base classes contain two data members with the same name. Each data member might hold different kinds of information in each of the two base classes and both be important for the derived class. Should both data members be inherited and if so how are they distinguished?

Some computer languages that allow their classes to engage in multiple inheritance attempt to solve the mentioned problems in more or less elegant ways. The designers of C# decided to deal with the problems by disallowing multiple inheritance and instead provide an alternative language construct called an interface.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata © 2008-2017.
If you may any questions please contact us: