![]() |
< Free Open Study > |
![]() |
6.3 A Valid Use of Multiple InheritanceWe 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.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.
And last but not least,
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. |
![]() |
< Free Open Study > |
![]() |