|< Free Open Study >|
6.7 DAG Multiple Inheritance
If an object-oriented design only uses single inheritance, the entire inheritance hierarchy can be displayed as a tree. If multiple inheritance is used, then a directed acyclic graph (i.e., a DAG) is needed to display the hierarchy. In some designs, the multiple inheritance hierarchy can produce a diamond-shaped structure, which overly complicates design as well as implementation. Consider Figure 6.9, the object-oriented design of a graduate student.
As suggested by the multiple inheritance heuristics, we first assume that we have made a mistake, and then we prove otherwise.
Now that we have fulfilled the necessary tests to ensure that multiple inheritance is a valid construct in this context, we are faced with a dilemma. How many names and social security numbers does a graduate student have in the real world? One, of course. Our graduate student has two, based on the semantics of inheritance, that is, a derived class gets a copy of its base class's data. This is an implementation problem for any language wishing to support multiple inheritance. It is important to note that we cannot always eliminate one copy of base class data when faced with class designs like the graduate student. Imagine that the person class had the data member's name and telephone number instead of name and social security number. As a graduate student I might be "Mr. Riel" with my office number when viewed as an instructor, but "Arthur Riel" with my home phone number when viewed as a student. This choice of design must be given to the users of any language supporting multiple inheritance.
In the C++ language, the keyword virtual is used with inheritance to distinguish those hierarchies willing to share the common base class (e.g., Person). Virtual inheritance in C++ creates a collection of unique problems for its users. Issues involving the order of constructor calls are particularly error prone. This form of inheritance complicates design and implementation, but it is necessary in some cases like the graduate student example. Designers should not be overly concerned with diamond-shaped multiple inheritance. This form of multiple inheritance is fairly rare in most object-oriented designs; therefore, the typical object-oriented designer will not have to deal with it often.
|< Free Open Study >|