22.3.3.1.  virtual Inheritance

[ fromfile: multiple-inheritance.xml id: virtualinheritance ]

In Figure 22.4, GraduateTeachingFellow inherited more than once from the same base class. Our focus in that section was on the possibilities for ambiguity and confusion. There is another problem with that model: redundancy. Instances of this multiply inherited class might look like Figure 22.5.

Figure 22.5. GradTeachingFellow - Nonvirtual

GradTeachingFellow - Nonvirtual

Person has attributes that should be inherited only once. It makes no sense for a GradTeachingFellow to have two birthdates and two names. virtual inheritance eliminates the redundancy.

The strange problems that can arise when multiple inheritance is used in controversial ways, especially with the added complexities of virtual versus non-virtual inheritance/functions, seem to have prompted the designers of Java to exclude multiple inheritance from their language. Instead, Java permits the programmer to define an interface, which consists only of abstract (pure virtual) functions. A Java class can then use the implements clause to implement as many interfaces as it needs.