Java Reference
In-Depth Information
Because of overloading, it is valid to define more than one method with
the same name. A call must select the ”right” definition, which informally
is the nearest accessible method definition in the inheritance hierarchy whose
parameters match the actual parameters provided in the call.
We begin semantic analysis of a call by gathering all themethod definitions
that might be targets of the call. This lookup process is guided by the kind of
method qualifier (if any) that is provided and the access mode of individual
methods.
If no qualifier is provided, thenwe examine the class (call it C )thatcontains
the call being analyzed. All methods defined within C with the selected
method name are accessible. In addition, methods defined in C 's superclasses
(its parent class, grandparent class, etc.) may be inherited depending on their
access qualifiers. Methods marked public or protected are always included,
but not private methods which cannot be inherited.
The qualifier, if non-null, also influences the selection of applicable meth-
ods. If the qualifier is the reserved word super,thenacallof M in class C
must reference a method inherited from a superclass (as defined above). (Use
of super in class Object is illegal, because Object has no superclass.)
If the qualifier is a type name T (which must be a class name), then a
call of M must reference a static method. (Instance methods are disallowed
because the object reference symbol this is undefined.) Public or protected
static methods may be referenced.
If the qualifier is an expression that computes an object of type T ,then T
must be a class marked public. A call of M may reference public or protected
methods within T and its superclasses.
These rules for selecting possible method definitions in a call are codi-
fied in
( ID ) returns
Attributes structures for all of the methods named ID in a given class. Simi-
larly,
get
M
ethods
(Figure 9.32). We assume that
method
D
efs
visible
M
ethods
( ID ) returns all the public and protectedmethods named
ID .Themethod
returns the class currently being compiled.
Once we have determined the set of definitions that are possible, we must
filter them by comparing each definition with the number and type of ex-
pressions that form the call's actual parameters. We will assume that each
method definition included in the set of accessible methods is represented
as an Attributes structure, which contains the fields returnType , signature ,
and classDe f inedIn . Field returnType is the type returnedbythemethod;
classDe f inedIn is the class in which the method is defined; signature is the type
signature of the method. We can build a types list for the actual parameters of
acallusingthe
get
C
urrent
C
lass
method defined in Figure 9.32, Marker 36 .
Once we have a type list for the actual parameters of a call, we must
compare it with the declared parameter type list of each method. A method
definition contains the field signature
get
A
rg
T
ypes
that records its parameter types and
Search WWH ::




Custom Search