0

I have a little confusion with Dynamic Binding in java. Here is a program, I want to know that, is there dynamic binding occurs or something else. What dynamic binding actually is?

class A {
int a;

public A() {
    a = 9;
}

public void show() {
    System.out.print("show in A ; ");
    System.out.println("a : " + a);
}
}
public class B extends A {
public B() {
    a = 8;
}

public void show() {
    System.out.print("show in B ; ");
    System.out.println("a : " + a);
}

public static void main(String[] args) {
    B p = new B();
    p.show();

    A q = new B();
    q.show();
}
}
0

5 Answers 5

4

It's here

 A q = new B();
 q.show();

Compiler uses virtual call instructions (invokeVirtual or invokeInterface) for methods which can be overriden (they cannot be static or private). In this code JVM detects that A.show is virtual and checks the actual type of q. Since it is B it calls B.show. If it were static JVM would call A.show and we would see

show in A
Sign up to request clarification or add additional context in comments.

Comments

2

I guess I confused you previously.

Dynamic (or late) binding is how polymorphism is implemented in Java. It occurs any time an instance method is invoked.

In your example, we are interested in the occurrences here

p.show();

and here

q.show();

At compilation time, the static type of the variable will be checked to see if the method show() is accessible, failing if it is not.

At runtime (dynamic), the run time (dynamic) type of the object will be checked to find an implementation of the method. If one is found, it is used, if not, the JVM keeps looking up the inheritance hierarchy.

For example

A q = new B();
q.show();

at run time, q is of type B and B overrides show() so B#show() is invoked.


In your answer I had commented on, the overriden method did not play a part. It was more a question of constructor execution order.

Comments

0

The method show() invoked in the below lines

A q = new B();
q.show();

The target object of the show method is determined at runtime.

Comments

0

you can change it in your main method to implement polymorphism or dynamic binding:

public static void main(String[] args) {
    A a = new A();       
    a.show();

    a = new B();       
    a.show();
}

as you can tell that even the reference is never changed A a but what it refer is changed, as different instance it point to, difference behavior will be done!
This is so called polymorphism.

Much powerful polymorphism, after using reflect in java will indulge you! have fun!

Comments

0

- Binding is act of method call identifying and calling its method body.

- Java only supports Dynamic Binding, except few exception cases.

- The compiler can't understand which method call to be associated with which method body... so its the responsibility of the method call to associated itself to its appropriate method body.

- Its very important to know that Fields (Instance Variables) are NOT POLYMORPHIC IN NATURE.

In your case the Dynamic binding is here:

 A q = new B();
 q.show();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.