Association of method call to the method body is known as binding. For example: This statement System.out.println("Hello");
is calling the method println()
. The body of the method present in the System class. The process of binding is associating this method call to the method body which is present in System class.
There are two types of binding: Static Binding that happens at compile time and Dynamic Binding that happens at runtime. Before I explain static and dynamic binding in java, lets see few terms that will help you understand this concept better.
What is reference and object?
It is important to understand the concept of reference and object in order to understand the process of binding.
class Human{
....
}
class Boy extends Human{
public static void main( String args[]) {
/* Here we are creating an object of Boy class
* obj1 is a reference and new Boy() is creating
* an object of Boy class and assigning to obj1
*/
Boy obj1 = new Boy();
/* Since class Boy extends Human class. The object creation
* can be done in this way. Parent class reference
* can have child class reference assigned to it
*/
Human obj2 = new Boy();
}
}
Static and Dynamic Binding in Java
As mentioned above, association of method call to the method definition is known as binding. There are two types of binding: Static binding and dynamic binding. Lets discuss them.
Static Binding or Early Binding
The binding which can be resolved at compile time by compiler is known as static or early binding. The binding of static, private and final methods is compile-time. Why? The reason is that the these method cannot be overridden and the type of the class is determined at the compile time. Lets see an example to understand this:
Static binding example
Here we have two classes Human and Boy, both of these classes have a static method walk(). This is an example of static binding as compiler knows that this method cannot be overriden so it has only one body which is defined in the class Human. The surety of the body makes the compiler works easy as it can make the binding during compilation itself.
class Human{ public static void walk() { System.out.println("Human walks"); } } class Boy extends Human{ public static void walk(){ System.out.println("Boy walks"); } public static void main( String args[]) { /* Reference is of Human type and object is * Boy type */ Human obj = new Boy(); /* Reference is of HUman type and object is * of Human type. */ Human obj2 = new Human(); obj.walk(); obj2.walk(); } }
Output:
Human walks Human walks
Dynamic Binding or Late Binding
When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method and in this case the type of the object determines which method is to be executed. The type of object is determined at the run time so this is known as dynamic binding.
Dynamic binding example
This is similar to what we have seen above. The only difference here is that in this example, overriding is actually happening since these methods are not static, private and final. In case of overriding the call to the overriden method is determined at runtime by the type of object thus late binding happens.
Here compiler is not sure which walk() method body needs to be invoked during method call as there are two variants of the same method is present, one in parent class and one in child class. This can only be determined during runtime, thus this binding happens at runtime.
Lets see an example to understand this:
class Human{ //Overridden Method public void walk() { System.out.println("Human walks"); } } class Demo extends Human{ //Overriding Method public void walk(){ System.out.println("Boy walks"); } public static void main( String args[]) { /* Reference is of Human type and object is * Boy type */ Human obj = new Demo(); /* Reference is of HUman type and object is * of Human type. */ Human obj2 = new Human(); obj.walk(); obj2.walk(); } }
Output:
Boy walks Human walks
As you can see that the output is different than what we saw in the static binding example, because in this case while creation of object obj the type of the object is determined as a Boy type so method of Boy class is called. Remember the type of the object is determined at the runtime.
Static Binding vs Dynamic Binding
Lets discuss the difference between static and dynamic binding in Java.
- Static binding happens at compile-time while dynamic binding happens at runtime.
- Binding of private, static and final methods always happen at compile time since these methods cannot be overridden. When the method overriding is actually happening and the reference of parent type is assigned to the object of child class type then such binding is resolved during runtime.
- The binding of overloaded methods is static and the binding of overridden methods is dynamic.
Ashish says
I was searching for static vs dynamic binding and found this, very helpful for me. Thank you so much for explaining these concepts with examples
Chaitanya Singh says
I am glad that you liked it. let me know if you have any question regarding binding in java, I would be happy to help you out.
Harry says
I understood the above points about early and late binding, however can you please explain binding in terms of references? It would be really helpful for me.
Chaitanya Singh says
Harry – let me explain your doubt with the help of few examples –
Static binding:
it means that the references are resolved during compile time by compiler
lets say –
Car obj = new Car();
obj.drive();
Compiler won’t face any difficulty while resolving the conflict during compilation.
Dynamic Binding:
Another example –
public void myMethod(Object obj){
((Car)obj).drive();
}
In this case compiler can’t resolve the object reference during compilation. Hence this can be resolved during run time only. This is called dynamic binding.
praveen says
Iamnot clear with the explaination actually do you mean dynamic polymorphism and dynamic binding are same
Akshay Hendre says
Hi Chaitanya, as you say in dynamic binding that, compiler gets confused while choosing method either from parent class or from child class. But how does it make right choice?
Chaitanya Singh says
Hi Akshay, References can only be resolved at runtime. Lets take the above example to understand this:
The fact that myobj is refers to the object of its child class Boy is resolved at runtime only. Hence, at compile time the compiler can’t be sure if the call to the method ‘walk()’ on these references actually refer to which version of the method – the super class version or the sub class version. That’s the reason such type of binding happens at runtime and known as dynamic binding.
Saddam Khan says
If the references variables are resolved at runtime then what will you call this below:
A ob = new ob():
ob.Show();
so here there is nothing ambiguous, so what should we call it early or late binding?
Rajani says
hi Chaitanya , your tutorial was very nice. Thank you so much for woderful explanation with examples.
Deeraj says
Very neatly explained, Thanks :)
Ahmad soory says
Hi
Thanks for this article
I have a question. What’s mean method binding??
Shahrukh Dilshad says
It is really helpful Thanks..
anshu says
this is helpful
srinivas says
very good example and content
Poonam says
Very well explained with examples.
Very helpful , informative and to the point answer.
Ameya says
Just the thing I was looking for… Thanks a lot!
Faezeh says
Hi,
Thank you for the explanation. I wrote a private method in my super class and I was able to override it in my subclass. I also, wrote a static method in my super class and I was able to override it in my subclass. Could you please tell me what do you mean by saying that static and private methods cannot be overridden?
Thanks
vikas says
i can say only one thing ………….. Entire Site is “AWESOME”.
Cam says
It is a wonderfully clear explanation!
Nandhakumar says
Hi
I have been confused for long time,after found this article i came to conclusion about binding..Super job ..tx a lot.
vikas says
Hi,
So am I right if I say – binding of non-private instance method is done at runtime. Is there any Java spec which confirms the same.
Subhadeep Das says
It’s said that Java uses static binding for overloaded methods. Please give an example to make it a bit clearer to me..
Saddam Khan says
I read your short article it is written quite nicely,
I want to ask what will you call the overloaded methods?
static binding or late binding?
according to you static binding comes where there is no ambiguity that which method to call, but in overriding a parent class variable is ambiguous to receive an object from which child class.
Don’t you think similarly in overloaded method its also ambiguous untill unless an argument is passed in runtime and certain method gets called,
Then why overloading is attributed as static binding? early binding?
veron says
I wasn’t getting a clear understanding of static and dynamic binding but after this article I’ve gotten a clear understanding of binding. tanx a lot
tanuja jain says
Is there any way in java through which we can find the debugging process of a program. I really need to know the compilation process about what get’s compiled first and what after that. Someboby please help me!
chandu says
Hello,
Its a nice artice.Have a question regarding resolving references.
I would like know what made the run time resolve the reference which compiler couldn’t do it.
I mean why does compiler is able to resolve the reference for
Dog d = new Dog()
and compiler cant resolve the reference for
Animal d = new Dog().
And why at run time references are resolved>Is it because of object creation?
luis says
Thanks for the explanation.
I have one question. If at the compile time, the compiler doesn’t know if the walk method is of Human or Boy, how does it know at runtime? Or if the compiler doesn’t know then who knows?
Thanks a lot!
luis says
This article on cpp binding will provide extra insights into binding. It helped me understand better.
Ravindra says
what is the benefit of dynamic binding over static one.. if we want to access the methods of sub class then we can make an object of the same with reference variable of sub class type…. what is the benefit if we want to call a method of sub class and using reference variable of super class type?
Aks says
Hi Chaitanya,
Thanks for your tutorial. In the example you have given
Human myobj = new Boy();
myobj.walk();
and the output will be Boy walks i.e walk method of Boy class is called. What if we want to call walk method of Human class ? How will we call a parent class method in a child class?
java says
u can call it with the help of super keyword
Praveen Chukkapalli says
I am very new to programming languages and I barely have knowledge about these. I’m just following your tutorial and able to understand very clearly. Thank you so much for this explanations….,
budy says
one of the best tutor of Java…
Neeraj says
Thanks Chaitanya, i have just started learning java, I got lot of my queries resolved by reading here.
Keep it up…
Thanks.
Dhivya says
public void myMethod(Object obj){
((Car)obj).drive();
}
What is meant by passing the object at runtime, in the above example, the object is passed at runtime , but it is possible only in method-overriding and not in method-overloading? pl do give a reply
Nithesh Reddy says
I am getting confused at overriding of static method. will they come under static polymorphism?
shefali says
static and dynamic binding is explained v nicely!!!
Thanks!!!!!
Rishika says
you have called the method as
Human myobj = new Boy();
why can’t i have
Boy myobj = new Boy();
why wouldn’t the compiler know that i am trying to access the child class. what is the exact need for dynamic binding?
Sebastian says
Thanks!
Perfectly explained! :)
Mano says
Never mind . My bad. Got it.
Sarvesh Gupta says
class A{
}
class B extends A{
public void func(){
System.out.println(“Funtion in B”);
}
public static void main(String[] g){
B b=new B();
b.func();
A a=new B();
a.func();
}
}
Author, please explain why compiler gives error here over Var type A.
I mean if the virtual methods(here func()) are bonded at runtime why is there an error here.
Its completely okay it func() was static or final or private but why with virtual func().
That might sound a silly question but I am confused.
Please help!
Chaitanya Singh says
Hello Sarvesh,
For this to work you must have func() method in parent class(class A) as well, otherwise it will throw compilation error as method func() is undefined for the class A.