WHAT IS THE DIFFERENCE BETWEEN STATIC BINDING AND DYNAMIC BINDING IN JAVA?

Samuel Fernandes

Total Post:38

Points:190
Posted by  Samuel Fernandes
Java 
 613  View(s)
Ratings:
Rate this:
  1. Mayank Tripathi

    Post:807

    Points:4035
    What is the difference between static binding and dynamic binding in java?

    Static binding:

    When type of the object is determined at compiled time(by the compiler), it is known as static binding.
    If there is any private, final or static method in a class, there is static binding.
    class Dog{  
     private void eat(){System.out.println("dog is eating...");}  
      
     public static void main(String args[]){  
      Dog d1=new Dog();  
      d1.eat();  
     }  

    Dynamic Binding
    When type of the object is determined at run-time, it is known as dynamic binding.
    class Animal{  
     void eat(){System.out.println("animal is eating...");}  
    }  
      
    class Dog extends Animal{  
     void eat(){System.out.println("dog is eating...");}  
      
     public static void main(String args[]){  
      Animal a=new Dog();  
      a.eat();  
     }  
    }

    Output:dog is eating...

    Here object type cannot be determined by the compiler, because the instance of Dog is also an instance of Animal.So compiler doesn't know its type, only its base type.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!