Java Reference
In-Depth Information
The
max()
method of the
MathUtil
class is overloaded. It has three definitions and each of its definitions
performs the same task of computing maximum, but on different types. The first definition computes a maximum of
two numbers of
int
data type, the second one computes a maximum of two floating-point numbers of
double
data
type, and the third one computes a maximum of an array of numbers of
int
data type. The following snippet of code
makes use of all three definitions of the overloaded
max()
method:
int max1 = MathUtil.max(10, 23); // Uses max(int, int)
double max2 = MathUtil.max(10.34, 2.89); // Uses max(double, double)
int max3 = MathUtil.max(new int[]{1, 89, 8, 3}); // Uses max(int[])
Note that method overloading gives you only sharing of the method name. It does not result in the sharing of
definitions. In Listing 1-7, the method name
max
is shared by all three methods, but they all have their own definition
of computing maximum of different types. In method overloading, the definitions of methods do not have to be
related at all. They may perform entirely different things and share the same name.
The following code snippet shows an example of operator overloading in Java. The operator is
+
. In the following
three statements, it performs three different things:
int n1 = 10 + 20; // Adds two integers
double n2 = 10.20 + 2.18; // Adds two floating-point numbers
String str = "Hi " + "there"; // Concatenates two strings
In the first statement, the + operator performs addition on two integers,
10
and
20
, and returns
30
. In the second
statement, it performs addition on two floating-point numbers,
10.20
and
2.18
, and returns
12.38
. In the third
statement, it performs concatenation of two strings and returns
"Hi there"
.
In overloading, the types of the actual method's parameters (types of operands in case of operators) are used
to determine which definition of the code to use. Method overloading provides only the reuse of the method name.
You can remove method overloading by simply supplying a unique name to all versions of an overloaded method.
For example, you could rename the three versions of the
max()
method as
max2Int()
,
max2Double()
, and
maxNInt()
.
Note that all versions of an overloaded method or operator do not have to perform related or similar tasks. In Java, the
only requirement to overload a method name is that all versions of the method must differ in number and/or type of
their formal parameters.
Coercion Polymorphism
Coercion is an ad hoc polymorphism. Coercion occurs when a type is implicitly converted (coerced) to another type
automatically even if it was not intended explicitly. Consider the following statements in Java:
int num = 707;
double d1 = (double)num; // Explicit conversion of int to double
double d2 = num; // Implicit conversion of int to double (coercion)
The variable
num
has been declared to be of
int
data type, and it has been assigned a value of
707
. The second
statement uses cast,
(double)
, to convert the
int
value stored in
num
to
double
, and assigns the converted value
to
d1
. This is the case of explicit conversion from
int
to
double
. In this case, the programmer makes his intention
explicit by using the cast. The third statement has exactly the same effect as the second one. However, it relies on
implicit conversion (called widening conversion in Java) provided by Java language that converts an
int
to
double
automatically when needed. The third statement is an example of coercion. A programming language (including Java)
provides different coercions in different contexts: assignment (shown above), method parameters, etc.