Java

Java – Static And Dynamic Binding

Binding is the process of connecting a method call to the method body. There are two forms of binding: static binding, which takes place during compilation, and dynamic binding, which occurs during execution. Before I go into detail about static versus dynamic binding in Java, let’s go through a few terminology that will clarify this idea.

What is reference and object?

class Human{
....
}
class Boy extends Human{
   public static void main( String args[]) {
       /*This statement simply creates an object of class
        *Boy and assigns a reference of Boy to it*/  
       Boy obj1 = new Boy();

       /* Since 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

Binding is the term for the connection between a method definition and method call, as was previously discussed. Dynamic binding and static binding are the two forms of binding. Let’s talk about them.

Static Binding or Early Binding

Static or early binding is defined as a binding that the compiler can resolve at compile time. Static, private, and final method binding occurs at compile time. Why? The reason is because the class’s type is decided at compile time and that these methods cannot be overridden. Here is an illustration to help you understand:

Static binding example

Here, there are two classes: Boy and Human. Although the walk() function is shared by both classes and cannot be overridden because it is static, even if I used an instance of the Boy class when creating object obj, the parent class method is still invoked. The reference is of Human kind, hence (parent class). Therefore, anytime a binding of static, private, and final methods occurs, the compiler determines the class type at build time, and the binding takes place right then.

lass 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

Such binding is characterized as dynamic or late binding when the compiler is unable to resolve the call or binding at build time. Method Overriding is a prime example of dynamic binding since it allows for the execution of the same method in both parent and child classes, depending on the object’s type. Dynamic binding is used when the type of an object is decided at runtime.

Dynamic binding example

The previous example is the same as this one. The only distinction is that because these methods are neither static, private, or final, overriding is really taking place in this scenario. When overriding, the type of the object determines the call to the overridden method at runtime, resulting in late binding. Here is an illustration to help you understand:

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

Let’s talk about the differences between Java’s static and dynamic binding.

  • Dynamic binding occurs during runtime as opposed to static binding, which occurs at compile time.
  • Since these methods cannot be overridden, binding of private, static, and final methods always takes place at the moment of compilation. The resolution of such binding occurs at runtime when method overriding is really occurring and the reference of the parent type is assigned to the object of the child class type.
  • Overloaded methods’ bindings are static, but overridden methods’ bindings are dynamic.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button