I'm pretty confused about the topic binding. As we know in static binding, the type of reference is checked to determine the binding, not the object it is referencing, and in dynamic binding, the type of object the reference is pointing is taken into consideration.
class A
{
void show()
{
System.out.println("From A");
}
}
class B extends A
{
void show()
{
System.out.println("From B");
}
}
class Main
{
public static void main(String[] quora)
{
A a1 = new A();
A a2 = new B();
a1.show(); //line 1
a2.show(); //line 2
}
}
In the above example, we can see during compile time, both Line1 and line2 will be treated by static binding and they will link to the A-class method show( since the type of reference is A). But during runtime, the call is resolved and line1 links to the A-class method show() and line2 links to the B-class method i.e, type of object or we can say dynamic binding.
So my main intention is to know the following.
Is dynamic binding always results after static binding? or I'm understanding something wrong?
If it's true then is it true to say every method is dynamically linked during the run time?
Can we generalise it?