26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

462 Object-Oriented <strong>Program</strong>ming Chapter 9<br />

Application class Test (Fig. 9.9) uses this Point/Circle inheritance hierarchy.<br />

The application begins in method main by instantiating Circle object circle1 (line<br />

11). This invokes the Circle construc<strong>to</strong>r at line 15 of Fig. 9.8, which immediately<br />

invokes the Point construc<strong>to</strong>r at line 15 of Fig. 9.7. The Point construc<strong>to</strong>r outputs the<br />

values received from the Circle construc<strong>to</strong>r by implicitly calling method <strong>to</strong>String<br />

and returns program control <strong>to</strong> the Circle construc<strong>to</strong>r. Then the Circle construc<strong>to</strong>r<br />

outputs the complete Circle by calling method <strong>to</strong>String. Notice that the first two<br />

lines of the output from this program both show values for x, y and radius. Polymorphism<br />

is once again causing the Circle’s <strong>to</strong>String method <strong>to</strong> execute because it is a<br />

Circle object that is being created. When <strong>to</strong>String is invoked from the Point construc<strong>to</strong>r,<br />

0.0 is displayed for the radius because the radius has not yet been initialized<br />

in the Circle construc<strong>to</strong>r.<br />

Circle object circle2 is instantiated next. Again, the Point and Circle construc<strong>to</strong>rs<br />

both execute. Notice, in the command-line output window, that the body of the<br />

Point construc<strong>to</strong>r is performed before the body of the Circle construc<strong>to</strong>r, showing that<br />

objects are constructed “inside out.”<br />

1 // Fig. 9.9: Test.java<br />

2 // Demonstrate when superclass and subclass<br />

3 // construc<strong>to</strong>rs and finalizers are called.<br />

4 public class Test {<br />

5<br />

6 // test when construc<strong>to</strong>rs and finalizers are called<br />

7 public static void main( String args[] )<br />

8 {<br />

9 Circle circle1, circle2;<br />

10<br />

11 circle1 = new Circle( 4.5, 72, 29 );<br />

12 circle2 = new Circle( 10, 5, 5 );<br />

13<br />

14 circle1 = null; // mark for garbage collection<br />

15 circle2 = null; // mark for garbage collection<br />

16<br />

17 System.gc(); // call the garbage collec<strong>to</strong>r<br />

18 }<br />

19<br />

20 } // end class Test<br />

Point construc<strong>to</strong>r: Center = [72, 29]; Radius = 0.0<br />

Circle construc<strong>to</strong>r: Center = [72, 29]; Radius = 4.5<br />

Point construc<strong>to</strong>r: Center = [5, 5]; Radius = 0.0<br />

Circle construc<strong>to</strong>r: Center = [5, 5]; Radius = 10.0<br />

Circle finalizer: Center = [72, 29]; Radius = 4.5<br />

Point finalizer: Center = [72, 29]; Radius = 4.5<br />

Circle finalizer: Center = [5, 5]; Radius = 10.0<br />

Point finalizer: Center = [5, 5]; Radius = 10.0<br />

Fig. Fig. 9.9 9.9 Order in which construc<strong>to</strong>rs and finalizers are called.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!