Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dynamic Mthod Lookup?

 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can any one explain why the compiler is giving error here?
Also expalin Dynamic Mthod Lookup in short so that i can check it with my understanding.
code:


------------------
azaman
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you get a compiler error because you override the method
int show() in class Super
with
long show() in class Sub.
for overriding the return types must be the same.
Dynamic method lookup means that " * at runtime * the method to invoke is chosen "
suppose you have
Super s = null;
// generate a random_number 0 or 1
if (random_number==0) {
s1 = new Super();
}
else {
s1 = new Sub();
}
// now call show() ....
s1.show();
At run time is decided wich show() is called.(Dynamic method lookup)
There is no way for the compiler to determine at compile time wich version of show() to invoke.
bye
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
To satisfy method overriding it must have identical method signature & return type.
To satisfy method overloading,It must differ in signature(i.e.name & formal parameter nos,order/type of para.).Only return type is not enough.I think it is nither method overriding
nor method overloading.


------------------
 
Ashik Uzzaman
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thnx Arie. . Yes i was trying to override show() and forgot that

* when overloading a method the retrun type is not considered
* when overriding a method the retrun type must be same

Ur understanding of Dynamic Method Lookup justifies mine...

------------------
azaman
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic