UML 2 For Dummies


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.


Категории