Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Static and Dynamic binding in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 362
    Comment on it

    Static Binding:

    It determines the the type of object at early stage during the compilation itself, i.e. it tells that the object belongs to which class during the compile time of the program. 

    There is no ambiguity in deciding the type of object in this case unlike the dynamic binding. Basically when we deal with private, static or final methods , static binding is there.

    Example:

    class Person{  
     private void run(){
    
      System.out.println("person is running");
      }  
      
     public static void main(String args[]){  
      Person person=new Person();  
      person.eat();  
     }  
    }  

    In the above program , We have created a method called run() inside the Person class and we have created the object of this class in the main class and called this method. Here it is clear that run() method of the Person class needs to be called and it is known at the compile time. So this is called static binding.

    Dynamic Binding:

    It determines the the type of object little late during the runtime, i.e. it tells that the object belongs to which class during the runtime. 

    It happens because there is an ambiguity to decide that object belongs to which class.

    Example:

    class Person{  
     public void run(){
     System.out.println("person is running");
    }  
    }  
      
    class Person2 extends Person{  
     public void run(){
     System.out.println("person 2 is running");  
    }
    }   
      
     public static void main(String args[]){  
      Person person=new Person2();  
      person.run();  
     }  
    }  

    Output:

    person 2 is running

    In this case the compiler is unable to decide the object type because instance of Person2 and Person1 will be the same, so there is ambiguity because of this and hence, this ambiguity is resolved during the run time of the program. So , this is called dynamic binding

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: