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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 02-Jan-2018 11:37:06
Advertisements