6.3 A Valid Use of Multiple Inheritance

 <  Free Open Study  >  

We have now seen the misuse of multiple inheritance. When is multiple inheritance a valid construct in an object-oriented design? Multiple inheritance is useful for capturing a relationship known as subtyping for combination . It is used to define a new class that is actually a special type of two other classes and those two base classes are from different domains. Consider the example in Figure 6.3, which describes one method for defining a wooden door.

Figure 6.3. A wooden door: an example of a material object.

graphics/06fig03.gif

We must first run through our three questions to ensure that our design is correct. Recall Heuristic 6.1, which states that if we have multiple inheritance in our design, we should assume we are making a mistake and should prove otherwise . In this example, our wooden door is made completely out of wood; do not worry about steel hinges and brass door knobs . We will look at that design of a door in the next example.

Is a wooden door a special type of door? Yes.

Is a door part of a wooden door? No.

Is a wooden door a special type of wooden object? Yes.

Is a wooden object part of a door? No.

And last but not least,

Is a wooden object a special type of door? No.

Is a door a special type of wooden object? No.

Since this design satisfies all of our heuristics, it is considered valid multiple inheritance.

Users of polymorphism should note that the wooden door designed above can only be viewed as a wooden object or a door via a given reference. If a wooden object reference is accessing the wooden door, then it knows about burn and split but has no knowledge of open and close . If a door reference is accessing the wooden door, then it knows about open and close , but not burn and split . For this reason, it is common for objects of classes derived from multiple classes to have access via multiple references of the different base classes (see Figure 6.4).

Figure 6.4. Multiple referencing of multiply derived objects.

graphics/06fig04.gif

 <  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