The availability of substantial and useful class libraries delivers the maximum benefitsof software reuse through inheritance. The standard Java class libraries that are shippedwith Java tend to be rather general purpose, encouraging broad software reuse. Many otherclass libraries exist.Reading subclass declarations can be confusing, because inherited members are notdeclared explicitly in the subclasses but are nevertheless present in them. A similar problemexists in documenting subclass members.Software Engineering Observation 9.6Java ensures that even if a constructor does not assign a value to an instance variable, thevariable is still initialized to its default value (e.g.,0for primitive numeric types,falseforbooleans,nullfor references).Software Engineering Observation 9.7Although inheriting from a class doesnotrequire access to the class’s source code,developers often insist on seeing the source code to understand how the class isimplemented. Developers in industry want to ensure that they’re extending a solid class—for example, a class that performs well and is implemented robustly and securely.Software Engineering Observation 9.8At the design stage in an object-oriented system, you’ll often find that certain classes areclosely related. You should “factor out” common instance variables and methods and placethem in a superclass. Then use inheritance to develop subclasses, specializing them withcapabilities beyond those inherited from the superclass.
9.7ClassObject3879.7ClassObjectAs we discussed earlier in this chapter, all classes in Java inherit directly or indirectly fromtheObjectclass (packagejava.lang), so its 11 methods (some are overloaded) are inher-ited by all other classes. Figure 9.12 summarizesObject’s methods. We discuss severalObjectmethods throughout this book (as indicated in Fig. 9.12).Software Engineering Observation 9.9Declaring a subclass does not affect its superclass’s source code. Inheritance preserves theintegrity of the superclass.Software Engineering Observation 9.10Designers of object-oriented systems should avoid class proliferation. Such proliferationcreates management problems and can hinder software reusability, because in a huge classlibrary it becomes difficult to locate the most appropriate classes. The alternative is tocreate fewer classes that provide more substantial functionality, but such classes mightprove cumbersome.MethodDescriptioncloneThisprotectedmethod, which takes no arguments and returns anObjectref-erence, makes a copy of the object on which it’s called. The default imple-mentation performs a so-calledshallow copy—instance-variable values in oneobject are copied into another object of the same type. For reference types,only the references are copied. A typical overriddenclonemethod’s imple-mentation would perform adeep copythat creates a new object for each ref-erence-type instance variable. Implementingclonecorrectly is difficult. Forthis reason, its use is discouraged. Many industry experts suggest that object