Exploring the Pros and Cons of Multiple Inheritances


You categorize classes in many different ways. A person class could be categorized by age, income, job role, or location. You use inheritance as a way to categorize your subclasses. If a subclass can inherit from one superclass, why not from two or more superclasses? Well, it can; UML allows you to show such multiple inheritance. For example, the MovieFilm class is an instance of RecordedMedia and of PhotoMedia. It should have all the attributes and operations of both superclasses, as illustrated in Figure 6-10. You show multiple inheritance in UML by connecting the subclass to each of its superclasses with a generalization relationship.

click to expand
Figure 6-10: Inheritance from classes.

Ah, but does the use of multiple inheritance make our programs richer? Sometimes. There are both advantages and disadvantages to using multiple inheritance. First, the advantages:

  • You categorize classes in many different ways. Multiple inheritance is a way of showing our natural tendency to organize the world. During analysis, for example, we use multiple inheritance to capture the way users classify objects.

  • By having multiple superclasses, your subclass has more opportunities to reuse the inherited attributes and operations of the superclasses.

Now for the disadvantages:

  • Some programming languages (such as Java) don’t allow you to use multiple inheritance. You must translate multiple inheritance into single inheritance or individual Java interfaces. This can be confusing and difficult to maintain because the implemented code for categorizing objects is quite different from the way the user organizes those objects. So, when the user changes their mind or adds another category, it is difficult to figure out how to program the new subclass.

  • The more superclasses your subclass inherits from, the more maintenance you are likely to perform. If one of the superclasses happens to change, the subclass may have to change as well.

  • When a single subclass inherits the same attribute or operation from different superclasses, you must choose exactly which one it must use. For example, the MovieFilm subclass inherits the place operation from both the RecordedMedia superclass and the PhotoMedia superclass. Remember, the RecordedMedia and PhotoMedia classes inherit the place operation from their superclass—ArchiveMedium. So now you must choose which method code for place to use for MovieFile—the one from RecordedMedia or the one from PhotoMedia. These choices can get very complex with multiple inheritance hierarchies. Be careful.

 Tip   During analysis, we use inheritance hierarchies to capture the way our users think about their world. During design, however, we try to stay away from multiple inheritance. In the long run, its disadvantages outweigh its advantages.




UML 2 for Dummies
UML 2 For Dummies
ISBN: 0764526146
EAN: 2147483647
Year: 2006
Pages: 193

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