Binding refers to the process of linking a method call with the method body (or actual method definition). In Java , method binding can happen at two different stages: at compile-time (static binding) and at run-time (dynamic binding).

1.Static Binding (Early Binding)

Definition:

  • Static binding occurs when the method call is resolved at compile time. In static binding, the compiler knows which method to call based on the type of the reference variable. Modifiers that are final, private, or static are bound using static binding because their behavior cannot be overridden.

Example:

class Animal {

    public static void sound() {

        System.out.println("Animal makes a sound");

    }

}




class Dog extends Animal {

    public static void sound() {

        System.out.println("Dog barks");

    }

}




public class Main {

    public static void main(String[] args) {

        Animal animal = new Dog(); // Reference type is Animal, object type is Dog

        animal.sound(); // Calls Animal's sound() due to static binding

    }

}

Output:

Animal makes a sound
  • Even though the object is of type Dog, the static method from Animal is called because static methods are bound at compile time based on the reference type (Animal).

Advantages:

  • Efficiency: Static binding is faster because the method call is resolved at compile time.
  • Simplicity: Easier to debug since the method to be invoked is known during compilation.

Uses:

Static binding is used with methods that cannot be overridden:

  • Static methods: Cannot be overridden, so they use static binding.
  • Private methods: Not visible to subclasses, so they are statically bound.
  • Final methods: Cannot be overridden, thus are bound at compile time.

2.Dynamic Binding (Late Binding)

Definition:

  • Dynamic binding occurs when the method call is resolved at runtime. In dynamic binding, the compiler does not know which method to invoke until the object is created. It is used with overridden methods where the actual method that gets called is determined by the runtime type of the object.

Example:

class Animal {

    public void sound() {

        System.out.println("Animal makes a sound");

    }

}




class Dog extends Animal {

    @Override

    public void sound() {

        System.out.println("Dog barks");

    }

}




public class Main {

    public static void main(String[] args) {

        Animal animal = new Dog(); // Reference type is Animal, object type is Dog

        animal.sound(); // Calls Dog's sound() due to dynamic binding

    }

}

Output:

Copy code

Dog barks

Advantages:

  • Flexibility: Allows runtime decisions based on the actual object type, making the system more flexible and extensible.
  • Polymorphism: Dynamic binding enables runtime polymorphism , where objects of different classes can be treated as objects of a common superclass, yet invoke their own methods.

Uses:

Dynamic binding is used with overridden methods to achieve runtime polymorphism:

  • Virtual methods in object-oriented programming.
  • Frameworks and libraries where method behavior is determined dynamically (e.g., GUI frameworks, web applications).
  • Method overriding in inheritance, where subclasses define specific behavior for inherited methods.