Difference between Early and Late Binding in Java

In this post, we will discuss the difference between early and late binding (static and dynamic binding) in Java.


 

Binding generally refers to a mapping of one thing to another. In the context of compiled programming languages, binding is the association of a method call with the method definition. In simpler terms, when a method is called in Java, the program control binds to the memory address where that method is defined.

 
There are two types of binding in Java – early (or static) binding and late (or dynamic) binding. This post provides an overview of the differences between the two.

  1. The early binding happens at the compile-time and late binding happens at the run time.
     
  2. In early binding, the method defination and the method call are linked during the compile time. This happens when all information needed to call a method is available at the compile time.

    Unlike early binding, the method calls are not resolved until run time in late binding as all information needed for a method call cannot be determined at compile time. So the method defination and a method call are not bound until run time.
     

  3. The normal method calls and overloaded method calls are examples of early binding while reflection and method overriding (run time polymorphism) are examples of late binding.
     
  4. The binding of private, static, and final methods happens at the compile time as they cannot be overridden.
     
  5. Since all information needed to call a method is available before run time, early binding results in faster execution of a program. While for later binding, a method call is not resolved until run time and this results in somewhat slower execution of code.
     
  6. The major advantage of late binding is its flexibility since a single method can handle different type of objects at run time. This significantly reduces the size of the code base and makes code more readable.

 

Example of early and late binding in Java:

 

Download   Run Code

Output:

Car Started
Vehicle Stopped

 
Consider above code. It has two classes – Vehicle class and Car class where the Car extends the Vehicle class. Each class has two methods – start() and stop(). The stop() method is static while the start() method is non-static. Now when we call start() and stop() methods using parent class reference, the start() method of the child class is called while stop() method of the parent class is called.

Why?

This is because stop() is a static method and hence cannot be overridden. So binding of stop() method happens at the compile time itself (early binding). On the other hand, start() is non-static method which is being overridden in the child class. So, the information about type of object is available at the run time only (late binding) and hence the start() method of the Car class is called.

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of