What is binding in Java?


Association of method call with the method body is known as binding in Java. There are two kinds of binding.

Static binding

In static binding the method call is bonded with the method body at compile time. This is also known as early binding. This is done using static, private and, final methods.

Example

class Super{
   public static void sample(){
      System.out.println("This is the method of super class");
   }
}
Public class Sub extends Super{
   Public static void sample(){
      System.out.println("This is the method of sub class");
   }
   Public static void main(String args[]){
      Sub.sample()
   }
}

Output

This is the method of sub class

Dynamic binding

In dynamic binding the method call is bonded with the method body at run time. This is also known as late binding. This is done using instance methods.

Example

class Super{
   public void sample(){
      System.out.println("This is the method of super class");
   }
}
Public class extends Super{
   Public static void sample(){
      System.out.println("This is the method of sub class");
   }
   Public static void main(String args[]){
      new Sub().sample()
   }
}

Output

This is the method of sub class

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 30-Jul-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements