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.

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

invoke method <strong>to</strong>String <strong>to</strong> append the String representation of the Circle. Lines<br />

39–40 append the area of the Circle <strong>to</strong> output.<br />

Next, the if/else structure at lines 43–48 attempts a dangerous cast in line 44. We cast<br />

point1, which refers <strong>to</strong> a Point object, <strong>to</strong> a Circle. If the program attempts <strong>to</strong> execute this<br />

statement, <strong>Java</strong> would determine that point1 really refers <strong>to</strong> a Point, recognize the cast <strong>to</strong><br />

Circle as being dangerous and indicate an improper cast with ClassCastException<br />

message. <strong>How</strong>ever, we prevent this statement from executing with the if condition<br />

if ( point1 instanceof Circle ) {<br />

that uses opera<strong>to</strong>r instanceof <strong>to</strong> determine whether the object <strong>to</strong> which point1 refers<br />

is a Circle. This condition evaluates <strong>to</strong> true only if the object <strong>to</strong> which point1 refers<br />

is a Circle; otherwise, the condition evaluates <strong>to</strong> false. Reference point1 does not<br />

refer <strong>to</strong> a Circle, so the condition fails, and a String indicating that point1 does not<br />

refer <strong>to</strong> a Circle is appended <strong>to</strong> output.<br />

If we remove the if test from the program and execute the program, the following<br />

message is generated at execution time:<br />

Exception in thread "main" java.lang.ClassCastException: Point<br />

at InheritanceTest.main(InheritanceTest.java:43)<br />

Such error messages normally include the file name (InheritanceTest.java) and<br />

line number at which the error occurred (43) so you can go <strong>to</strong> that specific line in the program<br />

for debugging.<br />

9.5 Construc<strong>to</strong>rs and Finalizers in Subclasses<br />

When an object of a subclass is instantiated, the superclass’s construc<strong>to</strong>r should be called<br />

<strong>to</strong> do any necessary initialization of the superclass instance variables of the subclass object.<br />

An explicit call <strong>to</strong> the superclass construc<strong>to</strong>r (via the super reference) can be provided as<br />

the first statement in the subclass construc<strong>to</strong>r. Otherwise, the subclass construc<strong>to</strong>r will call<br />

the superclass default construc<strong>to</strong>r (or no-argument construc<strong>to</strong>r) implicitly.<br />

Superclass construc<strong>to</strong>rs are not inherited by subclasses. Subclass construc<strong>to</strong>rs, however,<br />

can call superclass construc<strong>to</strong>rs via the super reference.<br />

Software Engineering Observation 9.10<br />

When an object of a subclass is created, first the subclass construc<strong>to</strong>r calls the superclass<br />

construc<strong>to</strong>r (explicitly via super or implicitly), the superclass construc<strong>to</strong>r executes, then<br />

the remainder of the subclass construc<strong>to</strong>r’s body executes. 9.10<br />

If the classes in your class hierarchy define finalize methods, the subclass<br />

finalize method as its last action should invoke the superclass finalize method <strong>to</strong><br />

ensure that all parts of an object are finalized properly if the garbage collec<strong>to</strong>r reclaims the<br />

memory for the object.<br />

The application of Fig. 9.7–Fig. 9.9 shows the order in which superclass and subclass<br />

construc<strong>to</strong>rs and finalizers are called. For the purpose of this example, class Point and<br />

class Circle are simplified.<br />

Class Point (Fig. 9.7) contains two construc<strong>to</strong>rs, a finalizer, a <strong>to</strong>String method<br />

and protected instance variables x and y. The construc<strong>to</strong>r and finalizer each print that<br />

they are executing, then display the Point for which they are invoked. Note the use of<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!