Java Reference
In-Depth Information
Private methods (since they are invoked only from inside the class
and are thus implicitly final)
n
In other scenarios, dynamic dispatch is meaningfully used. But what
exactly does dynamic dispatch mean?
Dynamic dispatch
means that the method that is appropriate for the object
being operated on is the one that is used. However, it does not mean that the
absolute best match is performed for all parameters. Specifically, in Java, the
parameters to a method are always deduced statically, at compile time.
For a concrete example, consider the code in Figure 4.45. In the
whichFoo
method, a call is made to
foo
. But which
foo
is called? We expect the answer
to depend on the run-time types of
arg1
and
arg2
.
Because parameters are always matched at compile time, it does not mat-
ter what type
arg2
is actually referencing. The
foo
that is matched will be
In Java, the param-
eters to a method
are always deduced
statically, at compile
time.
Static overloading
means that the
parameters to a
method are always
deduced statically,
at compile time.
public void foo( Base x )
The only issue is whether the
Base
or
Derived
version is used. That is the
decision that is made at run time, when the object that
arg1
references is known.
The precise methodology used is that the compiler deduces, at compile
time, the best signature, based on the static types of the parameters and the
methods that are available for the static type of the controlling reference. At that
point, the signature of the method is set. This step is called
static overloading
.
The only remaining issue is which class's version of that method is used. This
is done by having the Virtual Machine deduce the runtime type of this object.
Once the runtime type is known, the Virtual Machine walks up the inheritance
hierarchy, looking for the last overridden version of the method; this is the first
method of the appropriate signature that the Virtual Machine finds as it walks
up toward
Object
.
8
This second step is called
dynamic dispatch
.
Static overloading can lead to subtle errors when a method that is sup-
posed to be overridden is instead overloaded. Figure 4.46 illustrates a com-
mon programming error that occurs when implementing the
equals
method.
The
equals
method is defined in class
Object
and is intended to return
true
if two objects have identical states. It takes an
Object
as a parameter, and the
Object
provides a default implementation that returns
true
only if the two
objects are the same. In other words, in class
Object
, the implementation of
equals
is roughly
Dynamic dispatch
means that once
the signature of an
instance method is
ascertained, the
class of the method
can be determined
at run time based
on the dynamic
type of the invoking
object.
public boolean equals( Object other )
{ return this == other; }
8. If no such method is found, perhaps because only part of the program was recompiled, then
the Virtual Machine throws a
NoSuchMethodException
.
Search WWH ::
Custom Search