SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Object-oriented Design
Exercise:
Exercises
Question:22 | ISBN:9781118771334 | Edition: 6

Question

Explain why the Java dynamic dispatch algorithm, which looks for the method to invoke for a call obj.foo( ), will never get into an infinite loop.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The dynamic dispatch algorithm throws an runtime error if it not found in invoked class, or its super class and look for till it reaches the Object class

 

if doesn't find that method signature, it throws runtime error.

 

Explanation

 

If there is a class C which extends to class B, which extends to class A, 

if you create an object for class C and invoke a method on it, the dynamic dispatch algorithm looks same method signature on that class

if it's not found, the algorithm look in it's super class, i.e., class B if it was not there, it will check in it's super class till it reaches the Object class

if it's not found in the Object class too, in which case, it will throw runtime error.

 

The dynamic dispatch algoritm designed to throw an error in the runtime. it's not possible to look for that method in never ending loop.

 

 

0 0

Discussions

Post the discussion to improve the above solution.