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).
Contents
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 {
// Static method
public static void sound() {
System.out.println("Animal makes sound");
}
// Overloaded method (method overloading - static binding)
public void sound(String type) {
System.out.println("Animal " + type + " makes sound");
}
}
public class StaticBindingExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound(); // Static binding (compile time)
animal.sound("dog"); // Static binding (compile time)
}
}
Output:
Animal makes sound
Animal dog makes sound
- The method
sound()
is statically bound during compile time, and the appropriate method is called based on the reference type and parameters. - Method overloading (same method name, different parameter list) is resolved at compile time.
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 {
// Overridden method
public void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
// Overriding method
@Override
public void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
// Overriding method
@Override
public void sound() {
System.out.println("Cat meows");
}
}
public class DynamicBindingExample {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Reference of Animal
Animal myDog = new Dog(); // Reference of Animal, but object of Dog
Animal myCat = new Cat(); // Reference of Animal, but object of Cat
myAnimal.sound(); // Animal makes sound (resolved at runtime)
myDog.sound(); // Dog barks (resolved at runtime)
myCat.sound(); // Cat meows (resolved at runtime)
}
}
Output:
Animal makes sound
Dog barks
Cat meows
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.