Java Reference
In-Depth Information
returns true, a particular method definition can be used; otherwise, it is imme-
diately rejected as not applicable to the call being processed.
After filtering out thosemethoddefinitions that are not applicable (because
of an incorrect argument count or an argument type mismatch), we count the
number of method definitions still under consideration. If it is zero, we have
an invalid call (no accessible method can be called without error). If the count
is one, we have a correct call.
If two or more method definitions are still under consideration, then we
need to choose the most appropriate definition. Two issues are involved here.
First, if a method is redefined in a subclass, we want to use the redefinition.
For example, let method M() be defined in both classes C and D:
class C { void M() { ... } }
class D extends C { void M() { ... } }
If we call M() in an instance of class D,wewanttousethedefinitionofM in D,
even though C's definition is visible and type-correct.
Second, it may also happen that one definition of amethod M takes an object
of class A as a parameter, whereas another definition of M takes a subclass of A
as a parameter. An example of this is:
class A { void M(A parm) { ... } }
class B extends A { void M(B parm) { ... } }
Now consider a call M(b) in class B,whereb is of type B. Both definitions of
M are possible, since an object of class B may always be used where a parameter
of its parent class (A) is expected. In this case we prefer to use the definition of
M(B parm) in class B because it is a ”closer match” to the call M(b) that is being
analyzed.
We now formalize the notion of one method definition being a ”closer
match” than another. We define a method definition D to be more specific
than another definition E if D'sclassisbindabletoE's class and each of D's
parameters is bindable to the corresponding parameter of E. This definition
captures the notion that we prefer a method definition in a subclass to an
otherwise identical definition in a parent class (a subclass may be assigned
to a parent class, but not vica versa. Similarly, we prefer arguments that
involve a subclass over arguments that involve a parent class (as was the
case in the example of M(A parm) and M(B parms) used above). A method
more
, de f 2) that tests whether method definition de f 2ismore
specific than method definition de f 1 is presented in Figure 9.33, Marker 38 .
If we have more than one accessible method definition that matches a par-
ticular argument list in a call, then we will filter out less-specific definitions.
If, after filtering, only one definition remains (called the maximally specific
S
pecific
( de f 1
Search WWH ::




Custom Search