EXPRESSIONS
Compile Time Step 2: Determine Method Signature
15.11.2
void setColor(int color) { this.color = (byte)color; }
15.11.2.2
Choose the Most Specific Method
If more than one method is both accessible and applicable to a method invocation,
it is necessary to choose one to provide the descriptor for the run time method dis
patch. Java uses the rule that the
most specific
method is chosen.
The informal intuition is that one method declaration is more specific than
another if any invocation handled by the first method could be passed on to the
other one without a compile time type error.
The precise definition is as follows. Let
m
be a name and suppose that there
are two declarations of methods named
m
, each having
n
parameters. Suppose that
one declaration appears within a class or interface
T
and that the types of the
parameters are
T1
, . . . ,
Tn
; suppose moreover that the other declaration appears
within a class or interface
U
and that the types of the parameters are
U1
, . . . ,
Un
.
Then the method
m
declared in
T
is
more specific
than the method
m
declared in
U
if and only if both of the following are true:
T
can be converted to
U
by method invocation conversion.
Tj
can be converted to
Uj
by method invocation conversion, for all
j
from
1
to
n
.
A method is said to be
maximally specific
for a method invocation if it is
applicable and accessible and there is no other applicable and accessible method
that is more specific.
If there is exactly one maximally specific method, then it is in fact
the most
specific
method; it is necessarily more specific than any other method that is appli
cable and accessible. It is then subjected to some further compile time checks as
described in 15.11.3.
It is possible that no method is the most specific, because there are two or
more maximally specific method declarations. In this case, we say that the method
invocation is
ambiguous
, and a compile time error occurs.
15.11.2.3
Example: Overloading Ambiguity
Consider the example:
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
327
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved