DEV Community

loading...
Cover image for Static and Dynamic Binding

Static and Dynamic Binding

sumit profile image Sumit Singh Updated on ・3 min read

What does binding actually mean?
Let us understand with an example. Suppose today is the day you are going to propose to your partner, but you had wanted to make it special. To make it so, you reach early at the place look for the head waiter and instructed him/her to bring a cake, wine, etc... and also told the musician to play your favorite romantic song as soon as you propose to her (By saying those magical words). Now, that moment comes, she arrived and you proposed to her. Now, what will happen? Obviously, she will say YES (Just one piece of advice don't get friend zone buddy), but apart from this waiter will bring the cake, wine, etc... and the musician will change the song to the romantic one.

Now, the thing to notice is what made the waiter bring foodstuff and the musician to change the song? and yes our are thinking correct, your action of proposing to her made them do so, we can simply say that waiter's and musician's actions are bounded to your call (proposing).
In the world of coding, binding is referred to as a mechanism of creating a link between a method call and method actual implementation.

Types of binding
As per the polymorphism concept in OOPs, an object can have many different forms. Object forms can be resolved at compile time and run time. There are two types of binding:

  1. Static Binding
  2. Dynamic Binding

Static Binding
If linking between a method call and method implementation is resolved at compile time then we call it static binding
OR
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. Why? The reason is that these methods cannot be overridden and the type of the class is determined at the compile time.

public class FastFood {
   public void create() {
      System.out.println("Creating in FastFood class");
   }
}

public class Main {
   public static void main(String[] args) {
      //Static binding
      FastFood fastFood= new FastFood();
      fastFood.create();
   }
}
Enter fullscreen mode Exit fullscreen mode

Here we can observe that simply the object fastFood will be binded to the create method of the FastFood class in the compile time.

Dynamic Binding
If linking between a method call and method implementation is resolved at run time then it dynamic binding. The dynamic binding uses an object to resolve binding but static binding uses the type of the class and fields.
OR
When the compiler is not able to resolve the call/binding at compile-time, such binding is known as Dynamic or late Binding. Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have the same method and in this case, the type of the object determines which method is to be executed. The type of object is determined at the run time so this is known as dynamic binding.

public class FastFood {
   public void create() {
      System.out.println("Creating in FastFood class");
   }
}
public class Pizza extends FastFood {
   @Override
   public void create() {
      System.out.println("Creating in Pizza class");
   }
}
public class Main {
   public static void main(String[] args) {
      //Dynamic binding
      FastFood pza= new Pizza();
      pza.create();
   }
}
Enter fullscreen mode Exit fullscreen mode

Here we can observe that simply the object fastFood will be binded to the create method of the FastFood class in the compile time. But, that will remain no longer as during runtime, the compiler will get a new definition of the create method in the child class i.e. in the Pizza class (As Pizza object is created by FastFood class).

Now, some of you might wonder, what if the Pizza class doesn't have a definition of the create method. Here, no need to worry it will just simply use the create method of the FastFood class (as static binding has already occurred at the compile-time).

Important Points

  • private, final, and static members (methods and variables) use static binding while for virtual methods (In Java methods are virtual by default) binding is done during run time based upon run time object.
  • Static binding uses Type information for binding while Dynamic binding uses Objects to resolve binding.
  • Overloaded methods are resolved (deciding which method to be called when there are multiple methods with the same name) using static binding while overridden methods using dynamic binding, i.e, at run time.

Discussion (2)

pic
Editor guide
Collapse
thejscode profile image
Jaskirat Grewal

Thanks Dude. All my issues regarding polymorphism are now resolved.

Collapse
sumit profile image
Sumit Singh Author

I glad to hear that mate. Means alot.