Java Reference
In-Depth Information
requirements document states that users should have the option to supply values
for the maximum number of uses, for automatic expiration, or both. This indi-
cates that at least four different constructors with a parameter for a new pass-
word value are needed. Each constructor will have a String parameter for a new
password value. The first constructor will use default values for all other instance
variables, so only one parameter is passed. The second constructor will have an
additional int parameter for the maximum number of uses. The third construc-
tor will have a boolean parameter for automatic expiration. The fourth construc-
tor will have both the int parameter for the maximum number of uses and the
boolean parameter for automatic expiration. This is possible because a Java class
can have multiple constructors, each having the same name as the class.
As you learned in Chapter 5, the practice of defining more than one method
with the same name is called method overloading. When a program contains
multiple methods (whether they are constructors or not) all having the same
name, the methods are said to be overloaded .
When creating an object, only one constructor is called, so a means is
required by which the compiler can uniquely identify the indicated method. This
is done through the method signature , which is obtained from a combination
of the method name and the formal parameter list in the method header.
Because overloaded methods have the same name, they must have unique
parameter lists, either in the number of parameters used or in the order of the
data types. For example, the following two method headers are valid overloaded
methods.
public float calcCommission(String name, float commission)
public float calcCommission(float commission, String name)
Although the methods have the same names and number of parameters, each
parameter list has a different order of data types: the first has a String followed
by a float, while the second has a float followed by a String. The return data type
is not used in determining the method signature; therefore, changing only the
return data type would not result in overloaded methods with unique method
signatures. Because constructors do not have return data types and they all have
the same name, it is even more obvious that only unique parameter lists provide
unique signatures in overloaded methods.
Use Unique Parameter Lists for Overloaded Methods
Multiple methods may be defined with the same name, but they
must not have identical parameter lists. The method name plus
the parameter list form the method signature, which must be
unique.
Figure 9-9 displays the code for the four overloaded constructors for the
Password class. Within each constructor, a new ArrayList object named
pswdHistory is created. This object has a capacity indicated by the class variable,
maxHistory. Within each constructor, instance variables are assigned new values
as appropriate. The constructor in lines 27 through 31 will use default values for
all instance variables. The second constructor in lines 33 through 39 has an
Search WWH ::




Custom Search