Web site developers - 448 Object-Oriented Programming Chapter 9 The programmer and
448 Object-Oriented Programming Chapter 9 The programmer and designer concentrate on the big picture the commonality among objects in the system rather than the special cases. This process is called abstraction. If a procedural program has many closely related special cases, then it is common to see switch structures or nested if/else structures that distinguish among the special cases and provide the processing logic to deal with each case individually. We will show how to use inheritance and polymorphism to replace such switch logic with much simpler logic. We distinguish between the is a relationship and the has a relationship. Is a is inheritance. In an is a relationship, an object of a subclass type may also be treated as an object of its superclass type. Has a is composition (as we discussed in Chapter
. In a has a relationship, a class object has one or more objects of other classes as members. For example, a car has a steering wheel. A subclass s methods might need to access certain of its superclass s instance variables and methods. A crucial aspect of software engineering in Java is that a subclass cannot access the private members of its superclass. If a subclass could access the superclass s private members, this would violate information hiding in the superclass. Software Engineering Observation 9.1 A subclass cannot directly access private members of its superclass. Testing and Debugging Tip 9.1 Hiding private members is a tremendous help in testing, debugging and correctly modifying systems. If a subclass could access its superclass s private members, it would be possible for classes derived from that subclass to access that data as well, and so on. This would propagate access to what is supposed to be private data, and the benefits of information hiding would be lost throughout the class hierarchy. However, a subclass can access the public and protected members of its superclass. A subclass also can use the package access members of its superclass if the subclass and superclass are in the same package. Superclass members that should not be accessible to a subclass via inheritance are declared private in the superclass. A subclass can effect state changes in superclass private members only through public, protected and package access methods provided in the superclass and inherited into the subclass. [Note: We use protectedinstance variables in this chapter to demonstrate how they work. Several of the exercises in this chapter require that you use only private instance variables, to maintain encapsulation.] Software Engineering Observation 9.2 To preserve encapsulation, all instance variables should be declared private and should be accessible only via set and get methods of the class. A problem with inheritance is that a subclass can inherit methods that it does not need or should not have. It is the class designer s responsibility to ensure that the capabilities provided by a class are appropriate for future subclasses. Even when the superclass methods are appropriate for the subclasses, it is common for a sublcass to require the method to perform a task in a manner that is specific to the subclass. In such cases, the superclass method can be overridden (redefined) in the subclass with an appropriate implementation. Perhaps most exciting is the notion that new classes can inherit from abundant class libraries, such as those provided with the Java API. Organizations develop their own class Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01