398 Object-Based Programming (Make my own web site) Chapter 8 of the constructors
398 Object-Based Programming Chapter 8 of the constructors receive values for the hour, minuteand second, all the constructors call setTime with values for hour, minute and second and substitute zeros for the missing values to satisfy setTime s requirement of three arguments. Notice in particular the constructor at lines 42 45, which uses the hour, minuteand secondvalues of its argument timeto initialize the new Time2object. Even though we know that hour, minute and second are declared as private variables of class Time2, we are able to access these values directly by using the expressions time.hour, time.minuteand time.second. This is due to a special relationship between objects of the same class. When one object of a class has a reference to another object of the same class, the first object can access all the second object s data and methods. Class TimeTest4(Fig. 8.7) creates six Time2objects (lines 17 22) to demonstrate how to invoke the different constructors of the class. Line 17 shows that the no-argument constructor is invoked by placing an empty set of parentheses after the class name when allocating a Time2 object with new. Lines 18 22 of the program demonstrate passing arguments to the Time2 constructors. Remember that the appropriate constructor is invoked by matching the number, types and order of the arguments specified in the constructor call with the number, types and order of the parameters specified in each method definition. So, line 18 invokes the constructor at line 23 of Fig. 8.6. Line 19 invokes the constructor at line 30 of Fig. 8.6. Lines 20 21 invoke the constructor at line 36 of Fig. 8.6. Line 22 invokes the constructor at line 42 of Fig. 8.6. Note that each Time2constructor in Fig. 8.6 could be written to include a copy of the appropriate statements from method setTime. This could be slightly more efficient, because the extra call to setTime is eliminated. However, consider changing the representation of the time from three intvalues (requiring 12 bytes of memory) to a single int value representing the total number of seconds that have elapsed in the day (requiring 4 bytes of memory). Coding the Time2 constructors and method setTime identically makes such a change in this class definition more difficult. If the implementation of method setTimechanges, the implementation of the Time2constructors would need to change accordingly. Having the Time2constructors call setTimedirectly requires any changes to the implementation of setTimeto be made only once. This reduces the likelihood of a programming error when altering the implementation. Software Engineering Observation 8.14 If a method of a class already provides all or part of the functionality required by a constructor (or other method) of the class, call that method from the constructor (or other method). This simplifies the maintenance of the code and reduces the likelihood of an error if the implementation of the code is modified. It is also an effective example of reuse. 1 // Fig. 8.7: TimeTest4.java 2 // Using overloaded constructors 3 4 // Java extension packages 5 import javax.swing.*; 6 Fig. 8.7Using overloaded constructors to initialize objects of class Time2(part 1 Fig. of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services