Static and Dynamic Binding in Java

In Java, binding refers to the association of a method call with the method body. Two types of binding exist in the realm of Java programming: static binding and dynamic binding. These concepts are fundamental to the understanding of polymorphism and play a crucial role in the design and execution of Java programs. In this tutorial, we'll explore both static and dynamic binding, delve into their characteristics, and discuss their implications for Java developers.


Static Binding

The binding which can be resolved at compile time by the compiler is known as static or early binding.

The binding of static, private and final methods is compile time binding because these method cannot be overridden and the type of the class is determined at the compile time. These methods can only be accessed by the object of the local class. Therefore the binding of these methods always takes place during compilation.

Generally, we can say that overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding.


Characteristics of Static Binding

  • Compile-Time Resolution : The binding between the method call and method implementation is resolved during the compile-time phase.

  • Determined by Reference Type : The reference type of the object is used to determine which method will be called. This decision is made at compile time.

  • Efficient Execution : Static binding is generally more efficient in terms of execution speed, as the method resolution is done at compile time.

Differences between static and dynamic binding

Static Binding Dynamic Binding
It happens at compile time for which is referred to as early binding. It happens at runtime so it is also referred as late binding.
Method overloading is static binding. Method overriding is static binding.
Generally more efficient in terms of execution speed. May have a slight performance overhead due to runtime resolution.
The reference type is used to determine the method call. The actual type of the object is used to determine the method call.

Example program of static binding in java

class Shoe {
  public static void printDetails() {
    System.out.println("I am a Shoe");
  }
}

class Nike extends Shoe {
  public static void printDetails() {
    System.out.println("I am a Nike Shoe");
  }
}

public class StaticBinding {
  public static void main(String[] args) {
    // Creating object of super and sub class
    Shoe sh = new Shoe();
    Shoe nk = new Nike();

    // Calling printDetails methods
    sh.printDetails();
    nk.printDetails();
  }
}
Output
I am a Shoe
I am a Shoe

In the above program, we got the same output from the parent object as well as child object's invocation to printDetails() method due to following reason:

  • We have created one object of subclass and one object of the superclass with the reference of the superclass.

  • 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

When compiler is not able to resolve the method call at compile time, such binding is known as Dynamic binding. In such scenarios, th e compiler resolves the method call binding during the execution of the program.

Method Overriding in java is an example of Dynamic binding where both the parent class and the child class have the same method and the type of the object determines which method is going to be invoked.


Characteristics of Dynamic Binding

  • Runtime Resolution : The binding between the method call and method implementation is resolved at runtime.

  • Determined by Object Type : The actual type of the object is used to determine which method will be called. This decision is made at runtime.

  • Polymorphism : Dynamic binding is a key element of polymorphism, allowing objects of different types to be treated as objects of a common type.

class Shoe {
  public void printDetails() {
    System.out.println("I am a Shoe");
  }
}

class Nike extends Shoe {
  public void printDetails() {
    System.out.println("I am a Nike Shoe");
  }
}

public class DynamicBinding {
  public static void main(String[] args) {
    // Creating object of super and sub class
    Shoe sh1 = new Shoe();
    Shoe sh2 = new Nike();

    // Calling printDetails methods
    sh1.printDetails();
    sh2.printDetails();
  }
}
Output
I am a Shoe
I am a Nike Shoe

In this program, we can see that the output is different than the static binding program above due to following reason:

  • We have not declared the methods as static in the code.

  • Object type cannot be determined by the compiler, because the instance of Nike is also an instance of Shoe. Hence, compiler doesn't know its type, only its base type till it gets executed.


Conclusion

Understanding the concepts of static and dynamic binding is crucial for Java developers aiming to design efficient, flexible, and polymorphic systems. While static binding provides early error detection and can be more performant in certain scenarios, dynamic binding is essential for achieving polymorphism and creating adaptable designs. Both binding mechanisms have their use cases and trade-offs, and the decision to use one over the other depends on the specific requirements of the software being developed. By mastering the concepts of static and dynamic binding, Java developers can design elegant and efficient solutions that leverage the strengths of each binding.