• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Dynamic Method dispatch is called dynamic polymorphism?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. i want to know why Dynamic method dispatch is called Dynamic polymorphism and why the overloading is called static polymorphism. i know the both concepts but why one is static and other is dynamic?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading (static) is determined at compile time, and so which signature to call is fixed. Overriding (dynamic) happens at runtime, so which class's version of the method to call can change from one invocation to the next.
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys i posted the same question again. Please answer to the new question. Got many more related questions in the new one.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

midhuna peru wrote:Hey guys i posted the same question again.


Please don't do that. If you have further questions on the same topic, or need clarification, just continue in this thread.
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A lot of people don’t say dynamic polymorphism, because we don’t believe that overloading is a kind of polymorphism at all.
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i din't mean to post it agian but there was a server problem when i posted the question first time. I thought it's not posted. So posted my question again and when check my posts I see two.
Ok so here are my doubts

what i see is this topic is related to Static and Dynamic binding which again i din't get.
Static Binding is compile time, the other is run time...I din't get this.

Say class A has display() method, then
A ref1=new A();
ref1.display();
why is this static binding? what is dynamic binding then?

now lets say class C has display() method agian and is derived class of A. then,
A ref1=new C();
ref1.display();
is it dynamic or static?

Examples please....
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Medhuna,
First of all you need to understand overloading and overriding. The true polymorphism (or so-called dynamic) applies to overridden methods.
Small example:



This is overloading, as m1 is NOT overridden. See the output. So at compile-time itself, the method that will run is decided. [STATIC]

In case of overriding, [make the argument type of m1 in Demo class to Object] it will be decided at run-time, deciding upon object referenced by "t". [DYNAMIC]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

midhuna peru wrote:Sorry i din't mean to post it agian but there was a server problem when i posted the question first time. I thought it's not posted. So posted my question again and when check my posts I see two.
Ok so here are my doubts

what i see is this topic is related to Static and Dynamic binding which again i din't get.
Static Binding is compile time, the other is run time...I din't get this.



Static binding applies in the case of overloading.



Which signature gets used is deteremined at compile time. In this case, the compiler determines that the signature that gets used will be foo(Object), since that's the closest match (only match in this case) to the signature called. Since p is declared of type Parent, only methods that exist in Parent are eligible for consideration when this decision is made. No matter what p ends up pointing to at runtime--Parent or some subclass of Parent--the reference is of type Parent, so only Parent's signatures are considered, and whatever signature the compiler chooses is the exact one that will be called at runtime.

Dynamic binding applies in the case of overriding.



The compiler has already determined that the foo(Object) signature will be called. As stated above: Since c is declared of type Parent, only methods that exist in Parent are eligible for consideration when this decision is made, and foo(Object) is the only Parent method signature that matches. The compiler doesn't look at the = new Child() part for that decision. All it cares about is that c is of type Parent.

The dynamic part is that at runtime, the JVM determines which class's implementation of foo(Object) will be called. Since the actual object is a Child, and since Child overrides foo(Object), Child's implementation is called.

  • Static = compile time = overloading = which signature is called is deteremined at compile time based on which is the closest match among the signatures in the declared reference type.
  • Dynamic = runtime = overriding = which class's implementation of the exact signature from above is determined at runtime by the class of the object and whether it or any anceestor overrides that signature.

  •  
    midhuna peru
    Ranch Hand
    Posts: 48
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yah got this
     
    reply
      Bookmark Topic Watch Topic
    • New Topic