|
UML 2 for Dummies Authors: Chonoles M. J., Schardt J. A. Published year: 2006 Pages: 51-52/193 |
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.
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.
The really great thing about inheritance is the productivity you get through reuse of code. We’ve shown you an example of code reuse earlier in the chapter with the following method from the Transcript classes place operation:
public void place (StorageSpace on) { if (on.userAccess(editor)) //extension is here super.place(on); //then reuse superclass method }
Notice the third line says super.place(on); . We are reusing the place method that is located in the ArchiveMedium superclass.
Creating an inheritance hierarchy of classes helps you simplify your programming code. Object-oriented programs can loop through a set of objects that are from the same generalization set (based on the same superclass) without knowing which object of which subclass is being invoked. The object-oriented program simply invokes an operation defined on the superclass. The program does not have to worry which object is being invoked because they all share a common superclass and all subclasses inherit the superclass operations.
For example, if our program has a list of objects created from the Videotape, CompactDisc, MovieFilm , and Audiotape subclasses and these objects were all mixed up on the list, we could write a program to retrieve each object from the list and invoke the recommendPlaybackMachine operation on that object. Since each object inherits the recommendPlaybackMachine operation from the RecordedMedia superclass the right behavior will be invoked.
The real payoff for you is when you want to extend your software. You can add new subclasses to an inheritance hierarchy and not have to change code in other parts of your program. For example, suppose we added a DVD subclass to the RecordedMedia inheritance hierarchy. We would have to program the subclass to handle the recommendPlaybackMachine operation. Even though we added a whole new class to the software, we would not have the change that part of the object-oriented program that goes through that list of objects described in the previous paragraph. If we added an object created from the DVD subclass to the list the program would still just invoke the recommendPlaybackMachine operation just as before. No code changes in the existing program.
|
UML 2 for Dummies Authors: Chonoles M. J., Schardt J. A. Published year: 2006 Pages: 51-52/193 |
![]() UML 2.0 in a Nutshell (In a Nutshell (O'Reilly)) | ![]() Learning UML 2.0 | ![]() UML 2.0 Pocket Reference (Pocket Reference (O'Reilly)) | ![]() UML Weekend Crash Course | ![]() UML For The IT Business Analyst |
![]() UML 2.0 in a Nutshell (In a Nutshell (O'Reilly)) | ![]() Learning UML 2.0 |
![]() UML 2.0 Pocket Reference (Pocket Reference (O'Reilly)) | ![]() UML Weekend Crash Course |
![]() UML For The IT Business Analyst |