Static and Dynamic Binding in Java – Differences and Examples
Binding means an association of method call to the method definition. The picture below clearly shows what is binding. There are two types of Binding: Static and Dynamic Binding in Java.
If the compiler maps the method at compile-time, it is Static Binding or early binding. And, if the method is resolved at runtime, it is Dynamic Binding or late binding.
We have discussed the topic of Polymorphism in Java. Polymorphism in Java is one of the OOPs features that allows an object to occur in multiple forms. When a method exhibits polymorphism, the compiler maps the name of the method to the final implementation.
In this article, we will discuss how the static and dynamic binding in Java are different from each other. So let’s start with a detailed introduction about both of them.
What is Static Binding or Early Binding in Java
Static Binding or Early Binding in Java refers to a process where the compiler determines the type of object and resolves the method during the compile-time. Generally, the compiler binds the overloaded methods using static binding.
There is a fact that the binding of static, private, and final methods are always done during compile-time using static-binding.
Why is the binding of static, final and private methods always a static binding?
The reason for the binding of private, final and static methods during the compile-time is that the compiler determines the type of the class at the compile-time and therefore we can not override them during the runtime.
Another reason is that the static binding of methods provides better performance than the runtime binding. The compiler becomes aware of these methods and understands that method overriding is not possible with such methods.
These methods can only be accessed by the object of the local class. Therefore the binding of these methods always takes place during compilation.
Example of Static Binding in Java
Suppose we have two classes named Person and Teacher. The Teacher class extends the Person class. Both of these classes have the same methods called speak(). But, this method is static so we can not override it.
Therefore, even if we use the object of the Teacher class then also it calls the method of the Person class.
Code to understand the static binding in Java
package com.techvidvan.binding; class Person { public void speak() { System.out.println("Person speaks"); } } class Teacher extends Person { public static void speak() { System.out.println("Teacher speaks"); } } public class StaticBinding { public static void main( String args[ ]) { // Reference is of Person type and object is Teacher type Person obj = new Teacher(); obj.speak(); //Reference and object both are of Person type. Person obj2 = new Person(); obj2.speak(); } }
Output:
Person speaks
From the above code, we got the same output from the parent class. This happened because:
- The reference for the parent class and the child class is the same(Person). That is, a single object refers to both of them.
- Since the method is static, the compiler is aware that this method can not be overridden in the child class and it knows which method to call. Therefore there is no ambiguity and the output is the same for both cases.
Dynamic Binding or Late Binding in Java
When the compiler resolves the method call binding during the execution of the program, such a process is known as Dynamic or Late Binding in Java. We also call Dynamic binding as Late Binding because binding takes place during the actual execution of the program.
The best example of Dynamic binding is the Method Overriding where both the Parent class and the derived classes have the same method. And, therefore the type of the object determines which method is going to be executed.
The type of object is determined during the execution of the program, therefore it is called dynamic binding.
Example of Dynamic Binding in Java
We consider the same example that we took in the static binding. But this code differs from the above code and gives the different output as the actual method overriding is happening here.
The actual overriding takes place since the methods are not declared as static, private, and final. Let’s see an example to understand this:
Code to understand the Dynamic Binding in Java:
package com.techvidvan.binding class Person { public void speak() { System.out.println("Person speaks"); } } class Teacher extends Person { @Override public void speak() { System.out.println("Teacher speaks"); } } public class DynamicBinding { public static void main( String args[]) { //Reference and objects are of Person type. Person obj2 = new Person(); obj2.speak(); // Reference is of Person type and object is Teacher type Person obj = new Teacher(); obj.speak(); } }
Output:
Teacher speaks
From the above code, we got different output because:
- We have not declared the methods as static in the code.
- During compilation, the compiler has no idea of which method to call. This happens because the compiler doesn’t go according to the type of the object but it checks only according to the reference variable. Therefore the binding gets delayed to runtime, so the respective version of the speak() method will be called on the basis of the type of the object.
Differences between Static Binding and Dynamic Binding
S.N. | Static Binding | Dynamic Binding |
1. | A type of polymorphism that collects the information to call a method during the compile-time. | A type of polymorphism that collects the information to call a method at the runtime. |
2. | The binding happens at compile time. | The binding happens at runtime. |
3. | The actual object is not used for binding. | The actual object is used for binding. |
4. | It is also called early binding because binding happens during compilation. | It is also called late binding because binding happens at run time. |
5. | The execution speed is high. | The execution speed is slow. |
6. | Method overloading is the best example of static binding. | Method overriding is the best example of dynamic binding. |
7. | The methods which are private, static and final, show static binding because we can not override them. | The methods other private, static and final methods show dynamic binding because overriding is possible with these methods. |
Summary
Binding is an integral part of any Object oriented programming language. It is a crucial topic in any programming language and each programmer must know its working as well as its two forms that we discussed in this article.
Binding can be either Static or Dynamic.The form depends on at what time the method call gets bound to the method definition; either compile-time or during the runtime. We discussed both of these types with examples and the control flow.
In the end, we saw the differences between both static and Dynamic Binding in Java.
Thank you for reading our article. If you still have doubts related to Static and Dynamic Binding in Java, do ask in the comment section and our team will help you.