Static and Dynamic Binding in Java

Introduction

 
In this article, we discuss static and dynamic binding in Java.
 

What is binding

 
In simple words, linking or joining two things are called binding. In other words, simply joining two things or people.
 
In human beings, bindings are of various types, like emotional binding, financial binding, etcetera.
 
In terms of computers, connecting a method call to a method body is called binding.
 
It is of mainly two types:
  1. Static binding (in other words, called as early binding)
  2. Dynamic binding (in other words, called as late binding)
Their Type
 
1. Variable type
 
For example:
 
int d=150;
 
Here variable d is a type of int.
 
2. References have a type
  1. class Cat {  
  2.     public static void main(String args[]) {  
  3.         Cat c1 = new Cat();  
  4.     }  
  5. }  
Here c1 is an instance of the Cat class, but there is also an instance of Animal.
 

Static binding in Java

 
It is defined as, when we compile our program and an object type is determined then it is known as static binding or early binding. If there is any method in a class of final, private, or static type then it is static binding.
 
Example
  1. class Cat {  
  2.     private void eating() {  
  3.         System.out.println("Cat is eating");  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         Cat c1 = new Cat();  
  7.         c1.eating();  
  8.     }  
  9. }  
Output
 
Static binding in Java
 

Dynamic binding in Java

 
When we run our program and an object type is determined then it is known as dynamic binding.
 
Example
  1. class AnimalEx {  
  2.     public void eating() {  
  3.         System.out.println("Animal Start eating....");  
  4.     }  
  5. }  
  6. class CatEx {  
  7.     public void eating() {  
  8.         System.out.println("Cat start eating....");  
  9.     }  
  10.     public static void main(String args[]) {  
  11.         CatEx anml = new CatEx();  
  12.         anml.eating();  
  13.     }  
  14. }  
Output
 
Dynamic binding in Java


Similar Articles