In: Computer Science
Explain why the Java dynamic dispatch algorithm, which looks for the method to invoke for a call o.a(), will never get into an infinite loop.
Solution:
The dynamic dispatch algorithm is the algorithm that determines
what method is
invoked when a method call is made in a Java program. The algorithm
first
looks at the class upon which it was invoked to determine if that
class
contains a method with the specified method signature. If it does,
that
method is invoked. If that class does not contain a method with the
specified
method signature then the algorithm will look at the superclass to
see if it
does, and if it does it is invoked. This process of looking for the
method,
and then looking in the superclass for the method if it is not
found, continues
until either the method is found and invoked, or, we arrive at the
topmost
class with no matching method in which case a run-time error in
thrown.
Because of the structure of Java class hierarchies it is impossible
for the
dynamic dispatch algorithm to get into an infinite loop. Any method
call will
either eventually locate the desired method in the class hierarchy
or the
algorithm will arrive at the topmost class, the Object class. If
the method
is not found in the topmost class no further searching for the
method is done
and a run-time error is thrown.
#please consider my effort and give me a like....thank u....