Table of Contents
Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, when a function is called by the Java compiler the task of correctly locating the method for the respective function is binding. Depending on when the compiler is able to associate a method body with its method call, binding in java is classified into two main headers:
- Early Binding, also known as Static Binding
- Late Binding, also known as Dynamic Binding
Let us go through these two types in detail.
Early Binding or Static Binding
As mentioned earlier, binding refers to the process of associating or “binding” a method body with its call. Early or Static Binding refers to the association of these two entities at compile time by the Java Compiler. Static binding is used to associate any private, final or static method bodies with their method call statements prior any execution taking place. The best instance of early binding is method overloading.
Why are static final and private methods associated by Early Binding?
This is due to the fact that static, final or private methods cannot be overridden by any subclass and hence they are always referred to by a constant reference, i.e. the class in which they are defined. Due to this property of static, final or private methods, the java compiler can easily determine pre-execution that those methods can only be referenced by an object of the class they are defined in.
The following example shall demonstrate Static Binding.
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Car { public static void startEngine(){ System.out.println("Car Engine Started"); } } public class Porsche extends Car { public static void startEngine(){ System.out.println("Porche's Engine Started"); } public static void main(String args[]){ Car car1 = new Porsche(); Car car2 = new Car(); car1.startEngine(); car2.startEngine(); } } |
Output:
Car Engine Started
Analysis:
If we analyze the output of the above code, we notice that despite overriding the “startEngine” method in derived class Porsche and initializing the object “car1” with the reference of the class Porsche, the method “startEngine()” method was not overridden and printed the text of the superclass Car.
This was due to the fact that the static method “startEngine()” of the class Car cannot be overridden and was bound by the java compiler during compilation.
Now let us have a look at the other form of Binding:
Late Binding or Dynamic Binding
If the compiler is unable to determine which method call a particular method is bound during compilation, then it resorts to late binding or dynamic binding. The best instance of dynamic binding is Method Overriding. A subclass overrides the method of the super class and during execution, methods get associated with their respective references.
It may be noted here that that for Dynamic Binding to occur, it is imperative that the method to be overridden is not declared as static, final or private. Declaring the method with any of the above modifiers will prevent it from being overridden and java will resort to static binding as it can easily determine the parent reference.
Let us observe dynamic binding by using the previous example. Only this time, the methods will no longer be declared as “static”
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Car { public void startEngine(){ System.out.println("Car Engine Started"); } } public class Porsche extends Car { public void startEngine(){ System.out.println("Porche's Engine Started"); } public static void main(String args[]){ Car car1 = new Porsche(); Car car2 = new Car(); car1.startEngine(); car2.startEngine(); } } |
Output:
Car Engine Started
Analysis:
The difference in output is noticeable, as the first method call has successfully bound the reference of the class Porsche and called the overridden method “startEngine()”. As we have no longer used the static keyword as a modifier to the method, the java compiler could not find a reference to bind the method to during compilation and hence resorted to Dynamic binding and the type or reference of the method was determined at runtime.
Differences between Static and Dynamic Binding
Static Binding | Dynamic Binding |
---|---|
Static or Early Binding takes place at compile time. | Dynamic or Late Binding takes place during runtime |
Methods declared as private, static or final show static binding as they cannot be overridden and can be associated, during compilation | Methods which are public, protected or default show dynamic binding as they can be overridden and their reference is associated only at runtime |
Actual object reference is not used in early binding | Actual object reference is used |
Method overloading is the best example of Static or Early Binding | Method Overriding is the prime example of Dynamic or Late Binding |