Java Reference
In-Depth Information
When you overload a method name, each of the method definitions in the class must
have a different signature.
Signature
The
signature
of a method consists of the method name and the list of types for parameters
that are listed in the heading of the method name.
EXAMPLE
If a method has the heading
public int
computeSomething(
int
n1,
double
x1,
double
x2, String name);
then the signature is
computeSomething(
int
,
double
,
double
,String)
Note that the return type is not part of the method signature.
In Display 4.11, we also overloaded the method name
dateOK
so that there are two
different methods named
dateOK
. The two signatures for the two methods named
dateOK
are
dateOK(
int
,
int
,
int
)
dateOK(String,
int
,
int
)
Display 4.12 gives a simple example of a program using the overloaded method
name
setDate
. Note that for each invocation of a method named
setDate
, only
one of the definitions of
setDate
has a signature that matches the types of the
arguments.
Overloading
Within one class, you can have two (or more) definitions of a single method name. This
is called
overloading
the method name. When you overload a method name, any two
definitions of the method name must have different signatures; that is, any two definitions
of the method name either must have different numbers of parameters or some parameter
position must be of differing types in the two definitions.