The availability of substantial and useful class libraries delivers the maximum

The availability of substantial and useful class

This preview shows page 28 - 30 out of 35 pages.

The availability of substantial and useful class libraries delivers the maximum benefits of software reuse through inheritance. The standard Java class libraries that are shipped with Java tend to be rather general purpose, encouraging broad software reuse. Many other class libraries exist. Reading subclass declarations can be confusing, because inherited members are not declared explicitly in the subclasses but are nevertheless present in them. A similar problem exists in documenting subclass members. Software Engineering Observation 9.6 Java ensures that even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value (e.g., 0 for primitive numeric types, false for boolean s, null for references). Software Engineering Observation 9.7 Although inheriting from a class does not require access to the class’s source code, developers often insist on seeing the source code to understand how the class is implemented. 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.8 At the design stage in an object-oriented system, you’ll often find that certain classes are closely related. You should “factor out” common instance variables and methods and place them in a superclass. Then use inheritance to develop subclasses, specializing them with capabilities beyond those inherited from the superclass.
Image of page 28
9.7 Class Object 387 9.7 Class Object As we discussed earlier in this chapter, all classes in Java inherit directly or indirectly from the Object class (package java.lang ), so its 11 methods (some are overloaded) are inher- ited by all other classes. Figure 9.12 summarizes Object ’s methods. We discuss several Object methods throughout this book (as indicated in Fig. 9.12). Software Engineering Observation 9.9 Declaring a subclass does not affect its superclass’s source code. Inheritance preserves the integrity of the superclass. Software Engineering Observation 9.10 Designers of object-oriented systems should avoid class proliferation. Such proliferation creates management problems and can hinder software reusability, because in a huge class library it becomes difficult to locate the most appropriate classes. The alternative is to create fewer classes that provide more substantial functionality, but such classes might prove cumbersome. Method Description clone This protected method, which takes no arguments and returns an Object ref- erence, makes a copy of the object on which it’s called. The default imple- mentation performs a so-called shallow copy —instance-variable values in one object are copied into another object of the same type. For reference types, only the references are copied. A typical overridden clone method’s imple- mentation would perform a deep copy that creates a new object for each ref- erence-type instance variable. Implementing clone correctly is difficult. For this reason, its use is discouraged. Many industry experts suggest that object
Image of page 29
Image of page 30

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture