Web site hosting - Chapter 8 Object-Based Programming 395 grammer of a

Chapter 8 Object-Based Programming 395 grammer of a class can define the class s constructor, which is invoked each time the program instantiates an object of that class. Instance variables can be initialized implicitly to their default values (0for primitive numeric types, false for booleans and null for references), they can be initialized in a constructor of the class or their values can be set later after the object is created. Constructors cannot specify return types or return values. A class can contain overloaded constructors to provide a variety of means for initializing objects of that class. When a program instantiates an object of a class, the program can supply initializers in parentheses to the right of the class name. These initializers are passed as arguments to the class s constructor. This technique is demonstrated in the example of Section 8.7. We have also seen this technique several times previously as we created new objects of classes like DecimalFormat, JLabel, JTextField, JTextArea and JButton. For each of these classes, we have seen statements of the form ref = new ClassName( arguments ); where ref is a reference of the appropriate data type, new indicates that a new object is being created, ClassName indicates the type of the new object and arguments specifies the values used by the class s constructor to initialize the object. If no constructors are defined for a class, the compiler creates a default constructor that takes no arguments (also called a no-argument constructor). The default constructor for a class calls the default constructor for its superclass (the class it extends), then proceeds to initialize the instance variables in the manner we discussed previously. If the class that this class extends does not have a default constructor, the compiler issues an error message. It is also possible for the programmer to provide a no-argument constructor, as we showed in class Time1 and will see in the next example. If any constructors are defined for a class by the programmer, Java will not create a default constructor for the class. Good Programming Practice 8.4 When appropriate (almost always), provide a constructor to ensure that every object is properly initialized with meaningful values. Common Programming Error 8.6 If constructors are provided for a class, but none of the public constructors are no-argument constructors, and an attempt is made to call a no-argument constructor to initialize an object of the class, a syntax error occurs. A constructor can be called with no arguments only if there are no constructors for the class (the default constructor is called) or if there is a no- argument constructor. 8.7 Using Overloaded Constructors Methods of a class can be overloaded (i.e., several methods in a class may have exactly the same name as defined in Chapter 6, Methods). To overload a method of a class, simply provide a separate method definition with the same name for each version of the method. Remember that overloaded methods must have different parameter lists. Common Programming Error 8.7 Attempting to overload a method of a class with another method that has the exact same signature (name and parameters) is a syntax error. The Time1 constructor in Fig. 8.1 initialized hour, minute and secondto 0 (i.e., 12 midnight in universal time) with a call to the class s setTime method. Figure 8.6 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

Leave a Reply