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 floatingpoint 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
Dostları ilə paylaş: