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 over
loading.
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 over
riding.
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.