Hosting your own web site - 456 Object-Oriented Programming Chapter 9 the subclass version

456 Object-Oriented Programming Chapter 9 the subclass version is automatically called. We have actually been overriding methods in every applet in the book. When we extend JAppletto create a new applet class, the new class inherits versions of init and paint (and many other methods). Each time we defined initor paint, we were overriding the original version that was inherited. Also, when we provided method toString for many of the classes in Chapter 8, we were overriding the original version of toString provided by class Object. As we will see in Fig. 9.8, the super reference followed by the dot operator can be used to access the original superclass version of that method from the subclass. Note that class Circle s toString method (lines 44 48) overrides the Point class toStringmethod (lines 41 44 of Fig. 9.4). Class Point s toString method overrides the original toString method provided by class Object. Actually, every class inherits a toString method, because class Object provides the original toString method. This method converts an object of any class into a String representation and is sometimes called implicitly by the program (e.g., when an object is concatenated with a String). Circle method toString directly accesses the protected instance variables x and y that were inherited from class Point. Method toString uses the values of x and y as part of the Circle s String representation. Actually, if you study class Point s toString method and class Circle s toString method, you will notice that Circle s toString uses the same formatting as Point s toString for the Pointparts of the Circle. Also, recall Software Engineering Observation 8.14, indicating that, if a method exists that performs part of another method s task, call the method. Point s toString performs part of the task of Circle s toString. To call Point s toString from class Circle, use the expression super.toString() Software Engineering Observation 9.7 A redefinition of a superclass method in a subclass need not have the same signature as the superclass method. Such a redefinition is not method overriding; rather, it is an example of method overloading. Software Engineering Observation 9.8 Any object can be converted to a String with an explicit or implicit call to the object s toString method. Software Engineering Observation 9.9 Each class should override method toString to return useful information about objects of that class. Common Programming Error 9.4 It is a syntax error if a method in a superclass and a method in its subclass have the same signature but a different return type. The InheritanceTest application (Fig. 9.6) instantiates Point object point1 and Circle object circle1 at lines 18 19 in main. The String representations of each object are assigned to Stringoutputto show that they were initialized correctly (lines 21 22). See the first two lines in the output screen capture to confirm this. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply