What are differences between static binding and dynamic binding in Java?



Following are the notable differences between static and dynamic binding −

Static Binding

  • It occurs during compile time.
  • It is observed in private, static and, final methods.
  • It is known as early binding.

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

  • It occurs during run time.
  • It is observed in instance methods.
  • It is known as late binding.

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
Swarali Sree
Swarali Sree

I love thought experiments.


Advertisements