Http web server - 452 Object-Oriented Programming Chapter 9 9.4 Relationship between

452 Object-Oriented Programming Chapter 9 9.4 Relationship between Superclass Objects and Subclass Objects An object of a subclass can be treated as an object of its superclass. This makes possible some interesting manipulations. For example, despite the fact that objects of a variety of classes derived from a particular superclass might be quite different from one another, we can create an array of references to them as long as we treat them as superclass objects. But the reverse is not true: A superclass object cannot always be treated a subclass object. For example, a Shape is not always a Circle. However, an explicit cast can be used to convert a superclass reference to a subclass reference. This can be done only when the superclass reference is referencing a subclass object; otherwise, Java will indicate a ClassCastException an indication that the cast operation is not allowed. Exceptions are discussed in detail in Chapter 14. Common Programming Error 9.1 Assigning an object of a superclass to a subclass reference (without a cast) is a syntax error. Software Engineering Observation 9.4 If an object has been assigned to a reference of one of its superclasses, it is acceptable to cast that object back to its own type. In fact, this must be done in order to send that object any of its messages that do not appear in that superclass. Our first example consists of two classes. Figure 9.4 shows a Point class definition. Figure 9.5 shows a Circle class definition. We will see that class Circle inherits from class Point. Figure 9.6 shows application class InheritanceTest, which demonstrates assigning subclass references to superclass references and casting superclass references to subclass references. Every applet defined previously has used some of the techniques presented here. We now formalize the inheritance concept. In Chapter 3, we stated that every class definition in Java must extend another class. However, notice in Fig. 9.4 that class Point (line 4) does not use the extends keyword explicitly. If a new class definition does not extend an existing class definition explicitly, Java implicitly uses class Object (package java.lang) as the superclass for the new class definition. Class Object provides a set of methods that can be used with any object of any class. Software Engineering Observation 9.5 Every class in Java implicitly extends Object, unless it is specified otherwise in the first line of the class definition, in which case the class indirectly extends Object. Thus, class Object is the superclass of the entire Java class hierarchy. Let us first examine the Point class definition (Fig. 9.4). The public services of class Point include methods setPoint, getX, getY, toString and two Point constructors. The instance variables x and yof Point are specified as protected. This prevents clients of Point objects from accessing the data directly (unless they are classes in the same package), but enables classes derived from Point to access the inherited instance variables directly. If the data were specified as private, the nonprivate methods of Point would have to be used to access the data, even by subclasses. Note that class Point s toString method overrides the original toString method inherited from class Object. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply