464 Object-Oriented Programming Chapter 9 9.7 Software Engineering with Inheritance We can use inheritance to customize existing software. When we use inheritance to create a new class from an existing class, the new class inherits the attributes and behaviors of an existing class; then we can add attributes and behaviors or override superclass behaviors to customize the class to meet our needs. It can be difficult for students to appreciate the problems faced by designers and implementers on large-scale software projects in industry. People experienced on such projects will invariably state that a key to improving the software development process is encouraging software reuse. Object-oriented programming in general, and Java in particular, certainly does this. It is the availability of substantial and useful class libraries that delivers the maximum benefits of software reuse through inheritance. As interest in Java grows, interest in Java class libraries will increase. Just as shrink-wrapped software produced by independent software vendors became an explosive growth industry with the arrival of the personal computer, so, too, will the creation and sale of Java class libraries. Application designers will build their applications with these libraries, and library designers will be rewarded by having their libraries wrapped with the applications. What we see coming is a massive worldwide commitment to the development of Java class libraries for a huge variety of applications arenas. Software Engineering Observation 9.12 Creating a subclass does not affect its superclass’s source code or the superclass s Java byte- codes; the integrity of a superclass is preserved by inheritance. A superclass specifies commonality. All classes derived from a superclass inherit the capabilities of that superclass. In the object-oriented design process, the designer looks for commonality among a set of classes and factors it out to form desirable superclasses. Subclasses are then customized beyond the capabilities inherited from the superclass. Software Engineering Observation 9.13 Just as the designer of non-object-oriented systems should avoid unnecessary proliferation of functions, the designer of object-oriented systems should avoid unnecessary proliferation of classes. Proliferating classes creates management problems and can hinder software reusability, simply because it is more difficult for a potential user of a class to locate that class in a huge collection. The trade-off is to create fewer classes, each providing substantial additional functionality, but such classes might be too rich for certain users. Performance Tip 9.1 When creating a new class, inherit from the class closest to what you need i.e., the one that provides the minimum set of capabilities required for a new class to perform its tasks. Subclasses could inherit data and functionality that they will not use, in which case memory and processing resources may be wasted. Note that reading a set of subclass declarations can be confusing because inherited members are not shown, but inherited members are nevertheless present in the subclasses. A similar problem can exist in the documentation of subclasses. Software Engineering Observation 9.14 In an object-oriented system, classes are often closely related. Factor out common attributes and behaviors and place these in a superclass. Then use inheritance to form subclasses without having to repeat common attributes and behaviors. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01