Thursday, October 19, 2017

Binding(Early and Late binding)





No.

Static Binding (Early)

Dynamic Binding(Late)

1)
It is a binding that happens at compile time. It is a binding that happens at run time.
2)
Actual object is not used for binding.
Actual object is used for binding.
3)
It is also called early binding because binding happens during compilation.
It is also called late binding because binding happens at run time.
4)
Method overloading is the best example of static binding.

Method overriding is the best example of dynamic binding.
5)
Private, static and final methods show static binding. Because, they can not be overridden.
Other than private, static and final methods show dynamic binding. Because, they can be overridden.




Example of static binding

  1. class Dog{  
  2.  private void eat(){System.out.println("dog is eating...");}  
  3.   
  4.  public static void main(String args[]){  
  5.   Dog d1=new Dog();  
  6.   d1.eat();  
  7.  }  
  8. }  

Example of dynamic binding

  1. class Animal{  
  2.  void eat(){System.out.println("animal is eating...");}  
  3. }  
  4.   
  5. class Dog extends Animal{  
  6.  void eat(){System.out.println("dog is eating...");}  
  7.   
  8.  public static void main(String args[]){  
  9.   Animal a=new Dog();  
  10.   a.eat();  
  11.  }  







0 comments:

Post a Comment