Java Reference
In-Depth Information
definition ), we know it is the correct definition to use. Otherwise, the choice
of definition is ambiguous and we must issue an error message. The pro-
cess of filtering out less-specific method definitions is detailed in Figure 9.33,
Marker 39 , which defines the method
( methodDe f Set ).
After we have reduced the set of possible method definitions down to a
single definition, semantic analysis is almost complete. We must check for the
following special cases of method calls:
filter
D
efs
Method calls qualified by a class name (className.method)mustbeto
a static method.
A call to a method that returns void may not appear in an expression
context (where a value is expected).
The complete semantic analysis for a method call, as developed above, is
defined in Figure 9.34, Marker 40 .
As an example of howmethod calls are checked, consider the call of M(arg)
in method test:
class A { void M(A parm) {...}
void M() {...} }
class B extends A { void M(B parm) {...}
void test(B arg) {M(arg);}}
At the call of M(arg), three definitions of M are visible. All are accessible. Two
of the three (those that take one parameter) are applicable to the call. The
definition of M(B parm) in B is more specific than the definition of M(A parm)
in A, so it is selected as the target of the call.
In all of the rules used to select among overloaded definitions, it is im-
portant to observe that the result type of a method is never used to decide if
a definition is applicable. Java does not allow two method definitions with
thesamenamethathaveidenticalparameters,butdi
ff
erent result types, to
coexist. Neither do C
. For example, the following two definitions
force a multiple definition error:
or C
++
int add(int i, int j) {...}
double add(int i, int j) {...}
This form of overloading is disallowed because it significantly complicates the
process of deciding which overloaded definition to choose. Not only must
the number and types of arguments be considered, but also the context within
which result types are used. For example in:
 
Search WWH ::




Custom Search