Static Binding and Dynamic Binding in Java: To help the Java community, Java Developer and testers those are working with Java programming language for them, we have prepared a core Java tutorial posts. Where we have shared the complete post on polymorphism in Java and the different types. After discussing that, in this post, we are going to discuss another important concept of Java, which is used by every Java programmer. That’s why you should be aware of static and dynamic binding in Java Because it is directly related to the execution of Java program code.
What Is Binding?
As we have seen in Method overloading and Method Overriding, it’s always a little bit tricky to select or to find which one is going to be used in run time if there is more then one method of the same name or two-variable having the same.
We can resolve this issue by using static and dynamic binding, but for those Java learners who do not know what is binding? Let me told you in simple word “Binding is a process of linking which method or variable is going to be called as per the code [ Linking between a method call and method definition].”
Some of the bindings are resolved during compile-time, and some of the bindings are depended upon the object and are resolved during runtime when the object is available.
Types Of Binding
There are two types of Binding in Java:
- Static Binding Or Early Binding
- Dynamic Binding Or Late Binding
Static Binding Or Early Binding
The Binding, which is resolved at compile time by the JVM compiler, is called Static Binding Or Early Binding. The Binding for static, private, and final methods is completed at the compiled time because static, private, and final methods can not be overridden, and the class of those methods is determined at compile time.
Let’s take a look at a simple program to understand this concept more clearly:
package com.SoftwareTestingo.JavaBasics; class basevehicle { public static void speed() { System.out.println("The Speed Of Vehicle Class"); } } public class Static_Binding extends basevehicle { public static void speed() { System.out.println("The Speed of Static Binding Class"); } public static void main(String[] args) { //Obj is Reference & object of Base vehicle class and basevehicle obj=new basevehicle(); //obj1 is reference of base vehicle class and Object is of Static_Binding basevehicle obj1=new Static_Binding(); obj.speed(); obj.speed(); } }
Output:
The Speed Of Vehicle Class The Speed Of Vehicle Class
Dynamic Binding Or Late Binding
Sometimes the binding process is not completed at the compile-time and waiting for the object creation. After the creation of the object, binding is completed during the run time, such type of binding process is called Dynamic Binding Or Late Binding.
One of the perfect examples of Dynamic Binding is method overriding because in overriding, both a parent class and child class have the same method name. Still, the type of the object is determined which method is going to execute at run time. So its an example of dynamic binding.
package com.SoftwareTestingo.JavaBasics; class basevehicleclass { public void speed() { System.out.println("The Speed Of Vehicle Class"); } } public class Dynamic_Binding extends basevehicleclass { public void speed() { System.out.println("The Speed of Dynamic Binding Class"); } public static void main(String[] args) { //Reference & Object of Base Vehicle Class basevehicleclass obj=new basevehicleclass(); //Reference is of Base Vehicle Class & Object is of Dynamic Binding Class basevehicleclass obj1=new Dynamic_Binding(); obj.speed(); obj1.speed(); } }
Output:
The Speed Of Vehicle Class The Speed of Dynamic Binding Class
Ref: article
Leave a Reply