Binding in Java

This article will explain one of the concepts of Java, Binding, along with some simple examples related to the concept to make a clear understanding.

Introduction 

 
This article will explain one of the concepts of Java, Binding, along with some simple examples related to the concept to make a clear understanding.
  

Concept of Binding in Java

 
If you have more than one method of the same name or two variables of the same name then there will be confusion during the compile-time and runtime of which method or variable is to be used as the reference code. This problem can be handled by the binding concept. Actually a connection made between the method call and the method body is known as binding.
 
Types of Binding
  • Static binding (early binding)
  • Dynamic binding (late binding)
Types of Binding
 
Before proceeding further let's explain some basic definitions.
 
We need to understand the type of instances that are used in Java and they are variable, references, and object.
 

Variable type

 
int A=30; float B=2.4; char C="Rahul";
 
Here the three variables A, B, C are of different types.
 

Reference type

  1. class Electronics {  
  2. public static void main(String args[]) {  
  3. Electronics phone=new Electronics(); } }  
Here phone is a type of electronics. Phone is a reference of electronics.
 

Object type

  1. class Building {  
  2.  void structure() {  
  3.   System.out.println("10 floors");  
  4.  }  
  5. }  
  6. class Room extends Building {  
  7.  void structure() {  
  8.   System.out.println("B H K flat");  
  9.  }  
  10.  public static void main(String args[]) {  
  11.   Building b = new Room();  
  12.   b.structure();  
  13.  }  
  14. }  
Here nokia is an instance of the phone class, but it is also an instance of the electronics class.
 

Static binding

 
When the binding can be resolved or the object can be determined at compile time by the compiler, then it is known as static binding. It is also called early binding.
 
All final, static, and private methods and variables are resolved by the static binding.
 
All overloaded methods are resolved by the static binding. Static binding uses type (class in Java) information to resolve binding.
 
A simple example of static binding is as in the following:
  1. class Flower {}  
  2. class Rose extends Flower {  
  3.  void  
  4.  function() {  
  5.   System.out.println("Rose oil");  
  6.  }  
  7.  public static void main(String args[]) {  
  8.   Rose r = new Rose();  
  9.   r.function();  
  10.  }  
  11. }  
Output
 
Rose oil
 
Here we have created an object of the Rose class and called the function() method of the same class. Since there is no confusion here, the compiler is able to resolve the binding at the compile time.
 

Dynamic binding

 
When the binding can be resolved or the object can be determined at the runtime, then it is known as dynamic binding. It is also called late binding.
 
All virtual methods are bound during runtime based upon the runtime object. The dynamic binding uses the object to resolve the binding.
 
Actually, overridden methods are the best example of dynamic binding because there is an overriding of both the parent and child classes that have the same method. When calling the overridden method, the compiler gets confused between the parent and child class methods since both have the same name.
 
A simple example of Dynamic binding is as in the following:
  1. class Building {  
  2.  void structure() {  
  3.   System.out.println("10 floors");  
  4.  }  
  5. }  
  6. class Room extends Building {  
  7.  void structure() {  
  8.   System.out.println("B H K flat");  
  9.  }  
  10.  public static void main(String args[]) {  
  11.   Building b = new Room();  
  12.   b.structure();  
  13.  }  
  14. }  
Output
 
B H K flat
 
Since Room extends Building, Room is a subclass or child class and Building is the superclass or parent class. Both of these classes have the same name as the method, structure(). Since we have assigned the reference of the parent class to the child class object, during the call of the structure() method the compiler gets confused about which structure method to call.