What is Binding in Java?
The connecting (linking) between a method call and method body/definition is called binding in java.
Look at the below figure in which we have defined a class X. It has two methods m1() and m2().
The x.m1(); (method call) is linked with m1() method whereas, x.m2(); is linked with m2() method. This linking is called binding.
Types of Binding in Java
There are two types of binding in java. They are as follows:
1. Static Binding (also known as Early Binding).
2. Dynamic Binding (also known as Late Binding).
Let’s understand static binding and dynamic binding one by one with example programs.
Static Binding in Java
The binding that happens during compilation is called static binding in java. This binding is resolved at the compiled time by the compiler.
In static binding, the java compiler does not check the type of object to which a particular reference variable is pointing to it.
Java compiler just checks which method is going to be called through reference variable and method definition exists or not.
This binding is also known as early binding because it takes place before the program actually runs. An example of static binding is method overloading.
Let’s take an example program where we will calculate the addition of two and three numbers using the method overloading technique.
Program source code 1:
package methodOverloading; public class Addition { void add(int x, int y) { int sum = x + y; System.out.println("Sum of two numbers: " +sum); } void add(int x, int y, int z) { int sum = x + y + z; System.out.println("Sum of three numbers: " +sum); } public static void main(String[] args) { Addition a = new Addition(); a.add(10, 20); // Calling add() method with passing two argument values. a.add(20, 30, 40); // Calling add() method with passing three argument values. } }
Output: Sum of two numbers: 30 Sum of three numbers: 90
In the above preceding code, Java compiler did not check the type of object. It just checked only method call through reference variable and method definition.
If there is any private, final, or static method in a class, all these method binding are called static binding.
Program source code 2:
public class Lion { private void eat() { System.out.println("Lion eats flesh"); } static void live() { System.out.println("Lion lives in Jungle"); } public static void main(String[] args) { Lion l = new Lion(); l.eat(); Lion.live(); } }
Output: Lion eats flesh Lion lives in Jungle
Why binding of private, static, and final methods are always static binding?
Static binding is better performance-wise because java compiler knows that all such methods cannot be overridden and will always be accessed by object reference variable.
Hence, the compiler doesn’t have any difficulty in binding between a method call and method definition. That’s why binding for such methods is always static.
Dynamic Binding in Java
The binding which occurs during runtime is called dynamic binding in java. This binding is resolved based on the type of object at runtime. In dynamic binding, the actual object is used for binding at runtime.
Dynamic binding is also called late binding or runtime binding because binding occurs during runtime. An example of dynamic binding is method overriding.
Let’s take an example program based on dynamic binding in Java.
Program source code 3:
package dynamicBinding; public class Animal { void eat() { System.out.println("Animals eat both plants and flesh"); } } public class Lion extends Animal { void eat() { System.out.println("Lions eat flesh because they are carnivore"); } } public class DynamicBindingTest { public static void main(String[] args) { Animal a = new Animal(); a.eat(); // It will call eat() method of Animal class because the reference variable is pointing to object of animal class. Animal a1 = new Lion(); a1.eat(); // It will call eat() method of Lion class because the reference variable is pointing towards the object of Loin class. } }
Output: Animals eat both plants and flesh Lions eat flesh because they are carnivore
In the preceding example, object type cannot be determined by the compiler. Therefore, JVM resolved the method calls based on the type of object at runtime.
Difference between Static and Dynamic Binding in Java
There are the following differences between static and dynamic binding. They are as follows:
1. Static binding occurs at compile-time while dynamic binding occurs at runtime.
2. In static binding, actual object is not used whereas, actual object is used in the dynamic binding.
3. Static binding is resolved at compile time whereas, dynamic binding is resolved at runtime.
4. Static binding is also called late binding because it happens during compilation whereas, dynamic binding is called early binding because it happens during runtime.
5. An example of static binding is method overloading whereas, the example of dynamic binding is method overriding.
6. Private, static, and final methods show static binding because they cannot be overridden whereas, except private, static, and final methods, other methods show dynamic binding because they can be overridden.
Hope that this tutorial has covered almost all the important points related to static and dynamic binding in java with many example programs. I hope that you will have understood this easy topic and enjoyed it.
Thanks for reading!!!
Next ⇒ Exception Handling in Java
No comments:
Post a Comment