Archive for September, 2007

462 Object-Oriented Programming Chapter 9 Application class Test (Business web site)

Tuesday, September 4th, 2007

462 Object-Oriented Programming Chapter 9 Application class Test (Fig. 9.9) uses this Point/Circle inheritance hierarchy. The application begins in method main by instantiating Circle object circle1 (line 11). This invokes the Circle constructor at line 15 of Fig. 9.8, which immediately invokes the Pointconstructor at line 15 of Fig. 9.7. The Point constructor outputs the values received from the Circle constructor by implicitly calling method toString and returns program control to the Circle constructor. Then the Circle constructor outputs the complete Circle by calling method toString. Notice that the first two lines of the output from this program both show values for x, y and radius. Polymorphism is once again causing the Circle s toString method to execute because it is a Circle object that is being created. When toString is invoked from the Point constructor, 0.0is displayed for the radiusbecause the radiushas not yet been initialized in the Circleconstructor. Circle object circle2 is instantiated next. Again, the Point and Circle constructors both execute. Notice, in the command-line output window, that the body of the Pointconstructor is performed before the body of the Circleconstructor, showing that objects are constructed inside out. 1 // Fig. 9.9: Test.java 2 // Demonstrate when superclass and subclass 3 // constructors and finalizers are called. 4 public class Test { 5 6 // test when constructors and finalizers are called 7 public static void main( String args[] ) 8 { 9 Circle circle1, circle2; 10 11 circle1 = new Circle( 4.5, 72, 29 ); 12 circle2 = new Circle( 10, 5, 5 ); 13 14 circle1 = null; // mark for garbage collection 15 circle2 = null; // mark for garbage collection 16 17 System.gc(); // call the garbage collector 18 } 19 20 } // end class Test Point constructor: Center = [72, 29]; Radius = 0.0 Circle constructor: Center = [72, 29]; Radius = 4.5 Point constructor: Center = [5, 5]; Radius = 0.0 Circle constructor: Center = [5, 5]; Radius = 10.0 Circle finalizer: Center = [72, 29]; Radius = 4.5 Point finalizer: Center = [72, 29]; Radius = 4.5 Circle finalizer: Center = [5, 5]; Radius = 10.0 Point finalizer: Center = [5, 5]; Radius = 10.0 Fig. 9.9Order in which constructors and finalizers are called. Fig. 9. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting control panel - Chapter 9 Object-Oriented Programming 461 1 // Fig.

Monday, September 3rd, 2007

Chapter 9 Object-Oriented Programming 461 1 // Fig. 9.8: Circle.java 2 // Definition of class Circle 3 public class Circle extends Point { // inherits from Point 4 protected double radius; 5 6 // no-argument constructor 7 public Circle() 8 { 9 // implicit call to superclass constructor here 10 radius = 0; 11 System.out.println( “Circle constructor: ” + this ); 12 } 13 14 // Constructor 15 public Circle( double circleRadius, int xCoordinate, 16 int yCoordinate ) 17 { 18 // call superclass constructor 19 super( xCoordinate, yCoordinate ); 20 21 radius = circleRadius; 22 System.out.println( “Circle constructor: ” + this ); 23 } 24 25 // finalizer 26 protected void finalize() 27 { 28 System.out.println( “Circle finalizer: ” + this ); 29 super.finalize(); // call superclass finalize method 30 } 31 32 // convert the Circle to a String 33 public String toString() 34 { 35 return “Center = ” + super.toString() + 36 “; Radius = ” + radius; 37 } 38 39 } // end class Circle Fig. 9.8 Circleclass definition to demonstrate when constructors and finalizers are called. Common Programming Error 9.5 When an overridden method calls the superclass version of the same method, not using keyword super to reference the superclass s method causes infinite recursion, because the subclass method actually calls itself. Common Programming Error 9.6 Cascading super reference to refer to a member (method or variable) several levels up the hierarchy (as in super.super.x) is a syntax error. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

460 Object-Oriented Programming Chapter (Web site traffic) 9 this in the

Sunday, September 2nd, 2007

460 Object-Oriented Programming Chapter 9 this in the System.out.println calls to cause an implicit call to method toString. Notice the first line of the finalizemethod (line 23). Method finalize should always be defined as protected so subclasses have access to the method but classes that simply use Pointobjects do not. Class Circle (Fig. 9.8) derives from Pointand contains two constructors, a finalizer, a toStringmethod and protected instance variable radius. The constructor and finalizer each print that they are executing, then display the Circle for which they are invoked. Note that the Circle method toString invokes Point s toString via super(line 19). Software Engineering Observation 9.11 When a superclass method is overridden in a subclass, it is common to have the subclass version call the superclass version and do some additional work. In this scenario, the superclass method performs the tasks common to all subclasses of that class, and the subclass method performs additional tasks specific to a given subclass. 1 // Fig. 9.7: Point.java 2 // Definition of class Point 3 public class Point extends Object { 4 protected int x, y; // coordinates of the Point 5 6 // no-argument constructor 7 public Point() 8 { 9 x = 0; 10 y = 0; 11 System.out.println( “Point constructor: ” + this ); 12 } 13 14 // constructor 15 public Point( int xCoordinate, int yCoordinate ) 16 { 17 x = xCoordinate; 18 y = yCoordinate; 19 System.out.println( “Point constructor: ” + this ); 20 } 21 22 // finalizer 23 protected void finalize() 24 { 25 System.out.println( “Point finalizer: ” + this ); 26 } 27 28 // convert Point into a String representation 29 public String toString() 30 { 31 return “[” + x + “, ” + y + “]”; 32 } 33 34 } // end class Point Fig. 9.7 Pointclass definition to demonstrate when constructors and finalizers are called. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming 459 invoke method toString (Web server setup)

Sunday, September 2nd, 2007

Chapter 9 Object-Oriented Programming 459 invoke method toString to append the String representation of the Circle. Lines 39 40 append the area of the Circleto output. Next, the if/elsestructure at lines 43 48 attempts a dangerous cast in line 44. We cast point1, which refers to a Point object, to a Circle. If the program attempts to execute this statement, Java would determine that point1 really refers to a Point, recognize the cast to Circle as being dangerous and indicate an improper cast with ClassCastException message. However, we prevent this statement from executing with the if condition if ( point1 instanceof Circle ) { that uses operator instanceof to determine whether the object to which point1 refers is a Circle. This condition evaluates to true only if the object to which point1 refers is a Circle; otherwise, the condition evaluates to false. Reference point1 does not refer to a Circle, so the condition fails, and a String indicating that point1 does not refer to a Circle is appended to output. If we remove the if test from the program and execute the program, the following message is generated at execution time: Exception in thread “main” java.lang.ClassCastException: Point at InheritanceTest.main(InheritanceTest.java:43) Such error messages normally include the file name (InheritanceTest.java) and line number at which the error occurred (43) so you can go to that specific line in the program for debugging. 9.5 Constructors and Finalizers in Subclasses When an object of a subclass is instantiated, the superclass s constructor should be called to do any necessary initialization of the superclass instance variables of the subclass object. An explicit call to the superclass constructor (via the super reference) can be provided as the first statement in the subclass constructor. Otherwise, the subclass constructor will call the superclass default constructor (or no-argument constructor) implicitly. Superclass constructors are not inherited by subclasses. Subclass constructors, however, can call superclass constructors via the super reference. Software Engineering Observation 9.10 When an object of a subclass is created, first the subclass constructor calls the superclass constructor (explicitly via super or implicitly), the superclass constructor executes, then the remainder of the subclass constructor s body executes. If the classes in your class hierarchy define finalize methods, the subclass finalize method as its last action should invoke the superclass finalize method to ensure that all parts of an object are finalized properly if the garbage collector reclaims the memory for the object. The application of Fig. 9.7 Fig. 9.9 shows the order in which superclass and subclass constructors and finalizers are called. For the purpose of this example, class Point and class Circle are simplified. Class Point (Fig. 9.7) contains two constructors, a finalizer, a toString method and protected instance variables x and y. The constructor and finalizer each print that they are executing, then display the Point for which they are invoked. Note the use of Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

458 Object-Oriented Programming Chapter 9 54 System.exit( 0 (Tomcat web server)

Saturday, September 1st, 2007

458 Object-Oriented Programming Chapter 9 54 System.exit( 0 ); 55 } 56 57 } // end class InheritanceTest Fig. 9.6Assigning subclass references to superclass references (part 2 of 2). Fig. 9. Line 26 assigns circle1 (a reference to a subclass Circle object) to point2 (a superclass Pointreference). It is always acceptable in Java to assign a subclass reference to a superclass reference, because of the is a relationship of inheritance. A Circleis a Pointbecause class Circleextends class Point. Assigning a superclass reference to a subclass reference is dangerous, as we will see. Lines 28 29 append the result of point2.toString()to output. Interestingly, when point2 is sent the toString message, Java knows that the object really is a Circle, so it chooses the Circle class s toString rather than the Point class s toStringas you might have expected. This is an example of polymorphism and dynamic binding concepts we treat in depth later in this chapter. The compiler looks at the preceding expression and asks the question, Does the data type of the reference point2(i.e., Point) have a toStringmethod with no arguments? The answer to this question is yes (per Point s toString definition on line 41 of Fig. 9.4). The compiler simply checks the syntax of the expression and ensures that the method exists. At execution time, the interpreter asks the question, What type is the object to which point2refers? Every object in Java knows its own data type, so the answer to the question is point2 refers to a Circleobject. Based on this answer, the interpreter calls the toStringmethod of the actual object s data type class Circle s toString method. See the third line of the screen capture to confirm this. The two key programming techniques we used to achieve this polymorphism effect are 1. extending class Pointto create class Circle, and 2. overriding method toStringwith the exact same signature in class Pointand class Circle. Line 33 casts point2, which admittedly is referencing a Circleat this time in the program s execution, to a Circleand assigns the result to circle2(this cast would be dangerous if point2were really referencing a Point, as we will soon discuss). Then we use circle2 to append to output the various facts about circle2. Lines 35 36 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01