Java Reference
In-Depth Information
inti=1-add(2,3);
a semantic analyzer would have to conclude that the definition of add that
returns a double is inappropriate because a double, subtracted from 1 would
yield a double, which cannot be used to initialize an integer variable.
A few languages, such as Ada, do allow overloaded method definitions
that di
er only in their result type. An analysis algorithm that can analyze this
more general form of overloading may be found in [Bak82].
ff
Interface and Constructor Calls
In addition to methods, Java and C
allow calls to interfaces and constructors.
The techniques we have developed apply directly to these constructs. An in-
terface is an abstraction of a class specifying a set of method definitions without
their implementations. For purposes of semantic analysis, implementations
are unimportant. When a call to an interface is made, the methods declared in
the interface (and perhaps its superinterfaces) are searched to find all declara-
tions that are applicable. Once the correct declaration is identified, we can be
sure that a corresponding implementation will be available at runtime.
Constructors are similar to methods in definition and structure. Construc-
tors are called in object creation expressions (using new) and in other construc-
tors; they can never be called in expressions or statements. A constructor can
be recognized by the fact that it has no result type (not even void). Once a
constructor call is recognized as valid (by examining where it appears), the
techniques developed above to select the appropriate declaration for a given
call can be used.
Subprogram Calls in Other Languages
The chief di
ff
erence between method calls in Java and C
and subprogram
calls in languages such as C and C
is that subprograms need not appear
within classes. Rather, subprograms are defined at the global level (within a
compilation unit). Languages such as ML and Python also allow subprograms
to be declared locally, just like local variables and constants. Some languages
allow overloading; others require a unique declaration for a given name.
Processing calls in these languages follows the same pattern as in Java and
++
C
. Using scoping and visibility rules, possible declarations corresponding to
a given call are gathered. If overloading is disallowed, the nearest declaration
is used. Otherwise, a set of possible declarationsisgathered.Thenumberand
type of arguments in the call is matched against the possible declarations. If a
single suitable declaration is not selected, a semantic error results.
 
Search WWH ::




Custom Search