Constructors


Constructors are similar to methods but are not considered members of a class. They are used to initialize the data structure of objects. Constructors have the same name as the class in which they are declared. They have no return type. A constructor is automatically called when an object is created using the new keyword. The system will provide a default constructor that takes no parameters if no constructor is explicitly declared, but the default constructor is not available if any constructors are provided in the class definition. The general syntax for a constructor is shown here.

 [access] class_name (input_parameters) {    //  body of constructor } 

A constructor can be given public , protected , private , or default access. If a constructor is given public access, it can be called anywhere inside or outside of the class in which it is defined. A protected constructor is available anywhere in the package in which it is defined. Outside of the package in which it is defined, a protected constructor can only be called inside a subclass. A private constructor cannot be called outside of the class in which it is defined. If no access is specified, the constructor has default access and only classes in the same package can call it. Constructors are generally given public or protected access.

Constructors can be (and often are) overloaded, meaning that more than one version of the constructor is defined with different input parameter lists. The system decides which constructor to call based on the number and type of arguments provided. For more information on method overloading, see Chapter 9.

Later in this chapter we will discuss the concept of inheritance. One of the features of inheritance is that a class can inherit members defined in a superclass. When a subclass object is created, the superclass fields must be initialized . This is accomplished by having the subclass constructor invoke a superclass constructor using the super keyword followed by whatever arguments the superclass constructor might need.

 super(arguments); 

The superclass constructor call must be the first statement in the subclass constructor.

Example: Adding Constructors to a Class

Because the SimpleGas2.java program initializes its members when they are declared, every instance of SimpleGas2 will have the same values of pressure and temperature. To allow each SimpleGas2 instance to have a different pressure and temperature value, we can initialize those members using constructors. The SimpleGas3 class defines two constructors. The first is a constructor that takes no arguments and initializes the members to default values. The second takes two double values as arguments and can be used to customize the pressure and temperature values of a SimpleGas3 object.

 public class SimpleGas3 {   private double pressure;   private double temperature;   public SimpleGas3() {     pressure = 101325.0;     temperature = 273.0;   }   public SimpleGas3(double p, double t) {     pressure = p;     temperature = t;   }   public double getPressure() {     return pressure;   }   public double getTemperature() {     return temperature;   } } 

We will also rewrite the driver program. This time it will create two SimpleGas3 objects using the two-argument constructor. The pressure variable of each instance is given a different value.

 public class GasDriver3 {   public static void main(String args[]) {     SimpleGas3 gas1 = new SimpleGas3(21.95, 207.8);     SimpleGas3 gas2 = new SimpleGas3(1.56, 245.4);     System.out.println("pressure of Gas 1 is " +                         gas1.getPressure());     System.out.println("pressure of Gas 2 is " +                         gas2.getPressure());   } } 

Output ”

 pressure of Gas 1 is 21.95 pressure of Gas 2 is 1.56 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net