aspose file tools*
The moose likes Beginning Java and the fly likes Why Dynamic Method dispatch is called dynamic polymorphism? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Dynamic Method dispatch is called dynamic polymorphism?" Watch "Why Dynamic Method dispatch is called dynamic polymorphism?" New topic
Author

Why Dynamic Method dispatch is called dynamic polymorphism?

midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 24, 2012
Posts: 48
Hey guys i posted the same question again. Please answer to the new question. Got many more related questions in the new one.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36575
    
  16
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

Joined: Aug 24, 2012
Posts: 48
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....
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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]


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

    Joined: Aug 24, 2012
    Posts: 48
    yah got this
     
    wood burning stoves
     
    subject: Why Dynamic Method dispatch is called dynamic polymorphism?
     
    Similar Threads
    About principal characteristics of OOP
    Polymorphism in java
    Overloading vs Polymorphism
    overriding and polymorphism
    using static method in polymorphism