System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə31/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   27   28   29   30   31   32   33   34   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

overloading. Method overloading is one of the ways that Java implements
polymorphism.
In general, to overload a method, simply declare different versions of it. The compiler
takes care of the rest. You must observe one important restriction: the type and/or
number of the parameters of each overloaded method must differ. It is not sufficient for
two methods to differ only in their return types. (Return types do not provide sufficient
information in all cases for Java to decide which method to use.) Of course, overloaded
methods may differ in their return types, too. When an overloaded method is called,
the version of the method whose parameters match the arguments is executed.
Here is a simple example that illustrates method overloading:


This program generates the following output:


As you can see, ovlDemo( ) is overloaded four times. The first version takes no
parameters, the second takes one integer parameter, the third takes two integer
parameters, and the fourth takes two double parameters. Notice that the first two
versions of ovlDemo( ) return void, and the second two return a value. This is
perfectly valid, but as explained, overloading is not affected one way or the other by the
return type of a method. Thus, attempting to use the following two versions of
ovlDemo( ) will cause an error:
As the comments suggest, the difference in their return types is insufficient for the
purposes of overloading.
As you will recall from 
Chapter 2
, Java provides certain automatic type conversions.
These conversions also apply to parameters of overloaded methods. For example,
consider the following:


The output from the program is shown here:
In this example, only two versions of f( ) are defined: one that has an int parameter
and one that has a double parameter. However, it is possible to pass f( ) a byte,
short, or float value. In the case of byte and short, Java automatically converts them
to int. Thus, f(int) is invoked. In the case of float, the value is converted to double


and f(double) is called.
It is important to understand, however, that the automatic conversions apply only if
there is no direct match between a parameter and an argument. For example, here is
the preceding program with the addition of a version of f( ) that specifies a byte
parameter:
Now when the program is run, the following output is produced:


In this version, since there is a version of f( ) that takes a byte argument, when f( ) is
called with a byte argument, f(byte) is invoked and the automatic conversion to int
does not occur.
Method overloading supports polymorphism because it is one way that Java
implements the “one interface, multiple methods” paradigm. To understand how,
consider the following: In languages that do not support method overloading, each
method must be given a unique name. However, frequently you will want to implement
essentially the same method for different types of data. Consider the absolute value
function. In languages that do not support overloading, there are usually three or more
versions of this function, each with a slightly different name. For instance, in C, the
function abs( ) returns the absolute value of an integer, labs( ) returns the absolute
value of a long integer, and fabs( ) returns the absolute value of a floating­point value.
Since C does not support overloading, each function has to have its own name, even
though all three functions do essentially the same thing. This makes the situation more
complex, conceptually, than it actually is. Although the underlying concept of each
function is the same, you still have three names to remember. This situation does not
occur in Java, because each absolute value method can use the same name. Indeed,
Java’s standard class library includes an absolute value method, called abs( ). This
method is overloaded by Java’s Math class to handle all of the numeric types. Java
determines which version of abs( ) to call based upon the type of argument.
The value of overloading is that it allows related methods to be accessed by use of a
common name. Thus, the name abs represents the general action that is being
performed. It is left to the compiler to choose the correct specific version for a
Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   27   28   29   30   31   32   33   34   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə