6.7 DAG Multiple Inheritance

 <  Free Open Study  >  

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.

Figure 6.9. A graduate student: an example of DAG inheritance.

graphics/06fig09.gif

As suggested by the multiple inheritance heuristics, we first assume that we have made a mistake, and then we prove otherwise .

Is an instructor a special type of person? Yes.

Is a person part of an instructor? No.

Is a student a special type of person? Yes.

Is a person part of a student? No.

Is a graduate student a special type of student? Yes.

Is a student part of a graduate student? No.

Is a graduate student a special type of instructor? Yes.

Is an instructor part of a graduate student? No.

Is an instructor a special type of student? No.

Is a student a special type of instructor? No.

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  >  


Object-Oriented Design Heuristics
Object-Oriented Design Heuristics (paperback)
ISBN: 0321774965
EAN: 2147483647
Year: 1996
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net