DEV Community

PRIYA K
PRIYA K

Posted on

Dynamic Binding,Static Binding

Binding
Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java, object can have many different forms. Object forms can be resolved at compile time and run time.

Java Dynamic Binding

Dynamic binding refers to the process in which linking between method call and method implementation is resolved at run time (or, a process of calling an overridden method at run time). Dynamic binding is also known as run-time polymorphism or late binding. Dynamic binding uses objects to resolve binding.

In Dynamic binding compiler doesn't decide the method to be called. In overriding both parent and child classes have the same method.

example
Methods are not static.
During compilation, the compiler has no idea as to which print has to be called since the compiler goes only by referencing variable not by the type of object, and therefore the binding would be delayed to runtime and therefore the corresponding version of the print will be called based on type on an object.

Characteristics of Java Dynamic Binding

  • Linking − Linking between method call and method implementation is resolved at run time.
  • Resolve mechanism − Dynamic binding uses object type to resolve binding.
  • Example − Method overriding is the example of Dynamic binding.
  • Type of Methods − Virtual methods use dynamic binding.

Example of Java Dynamic Binding
we've created two classes Animal and Dog where Dog class extends Animal class. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.

package com.tutorialspoint;
class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");
   }
}
public class Tester {
   public static void main(String args[]) {
      Animal a = new Animal();   // Animal reference and object
      // Dynamic Binding      
      Animal b = new Dog();   // Animal reference but Dog object
      a.move();   // runs the method in Animal class
      b.move();   // runs the method in Dog class
   }
}
Enter fullscreen mode Exit fullscreen mode

Output
Animals can move
Dogs can walk and run

Explanation:
b is a type of Animal it runs the move method in the Dog class. The reason for this is: In compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.

the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object.

Java Dynamic Binding: Using the super Keyword
When invoking a superclass version of an overridden method the super keyword is used so that we can utilize parent class method while using dynamic binding.

Example: Using the super Keyword

we've created two classes Animal and Dog where Dog class extends Animal class. Dog class overrides the move method of its super class Animal. But it calls parent move() method using super keyword so that both move methods are called when child method is called due to dynamic binding. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal {
   public void move() {
      super.move();   // invokes the super class method
      System.out.println("Dogs can walk and run");
   }
}
public class TestDog {
   public static void main(String args[]) {
      Animal b = new Dog();   // Animal reference but Dog object
      b.move();   // runs the method in Dog class
   }
}
Enter fullscreen mode Exit fullscreen mode

Output
Animals can move
Dogs can walk and run

Java Static Binding
Static binding refers to the process in which linking between method call and method implementation is resolved at compile time. Static binding is also known as compile-time binding or early binding.

The binding which can be resolved at compile time by the compiler is known as static or early binding. The binding of all the static, private, and final methods is done at compile-time.

example
We have created one object of subclass and one object of the superclass with the reference of the superclass.
Since the print method of the superclass is static, the compiler knows that it will not be overridden in subclasses and hence compiler knows during compile time which print method to call and hence no ambiguity.

Characteristics of Java Static Binding

  • Linking − Linking between method call and method implementation is resolved at compile time.
  • Resolve mechanism − Static binding uses type of the class and fields to resolve binding.
  • Example − Method overloading is the example of Static binding.
  • Type of Methods − private, final and static methods and variables uses static binding.

Example of Java Static Binding

we've created a Calculator class having two static methods with same name but different arguments to add two and three int values respectively. In main() method, we're calling these methods and printing the result. Based on the number of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.

package com.tutorialspoint;
class Calculator{
   public static int add(int a, int b){
      return a + b;
   }
   public static int add(int a, int b, int c){
      return a + b + c;
   }
}
public class Tester {
   public static void main(String args[]){
      System.out.println(Calculator.add(20, 40));
      System.out.println(Calculator.add(40, 50, 60));
   }
}
Enter fullscreen mode Exit fullscreen mode

Output
60
150

*why binding of static, final, and private methods is always static binding? *

Static binding is better performance-wise (no extra overhead is required). The compiler knows that all such methods cannot be overridden and will always be accessed by objects of the local class. Hence compiler doesn’t have any difficulty determining the object of the class (local class for sure). That’s the reason binding for such methods is static. 
Enter fullscreen mode Exit fullscreen mode

differences between static and dynamic binding **
**Static Binding

  • It takes place at compile time for which is referred to as early binding
  • It uses overloading more precisely operator overloading method .Overloaded methods are resolved (deciding which method to be called when there are multiple methods with the same name) using static binding
  • It takes place using normal functions
  • private functions, final ,const and static members (methods and variables) use real objects in static binding.
  • The static binding uses Type information for binding

Dynamic Binding
It takes place at runtime so do it is referred to as late binding.
It uses overriding methods.
It takes place using virtual functions.virtual methods (In Java methods are virtual by default) binding is done during run time based upon the run time object.
Real objects use dynamic binding.
Dynamic binding uses Objects to resolve to bind.

Top comments (0)