Monday, February 25, 2013

What is Static and Dynamic binding in Java


What is Static and Dynamic binding in Java with Example

Static and dynamic binding  in Java are two important concept which Java programmer should be aware of. this is directly related to execution of code. If you have more than one method of same name (method overriding) or two variable of same name in same class hierarchy it gets tricky to find out which one is used during runtime as a result of there reference in code. This problem is resolved using static and dynamic binding in Java. For those who are not familiar with binding operation, its process used to link which method or variable to be called as result of there reference in code. Most of the references is resolved during compile time but some references which depends upon Object and polymorphism in Java is resolved during runtime when actual object is available. In this Java tutorial we will see some examples of static and dynamic binding and differences between static binding and dynamic binding in Java.

Difference between Static and Dynamic binding in Java:

Difference between static and dynamic binding is a popular Java programming interview question which tries to explore candidates knowledge on have compiler and JVM finds which methods to call if there are more than one method of same name as it's case in method overloading and overriding. This is also best way to understand what is static binding and what is dynamic binding in Java. In next section we will difference between both of them.

Static Binding vs Dynamic binding Java

Here are few important difference between static and dynamic binding in Java written in point format. knowledge of static and dynamic binding is require to understand Java code and find out any bugs and issue while running Java program. It also helps in troubleshooting and debugging in Java.

1) Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

2) privatefinal and static methods and variables uses static binding and bonded by compiler while virtual methods are bonded during runtime based upon runtime object.

3) Static binding uses Type(Class in Java information for binding while Dynamic binding uses Object to resolve binding.

3) Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime. Here is an example which will help you to understand both static and dynamic binding in Java.

Static Binding Example in Java

Here is an example of static binding in java, which will clear things on how overloaded methods in java are bonded during compile time using Type information.

public class StaticBindingTest {
 
    public static void main(String args[])  {
       Collection c = new HashSet();
       StaticBindingTest et = new StaticBindingTest();
       et.sort(c);
     
    }
   
    //overloaded method takes Collection argument
    public Collection sort(Collection c){
        System.out.println("Inside Collection sort method");
        return c;
    }
 
   
   //another overloaded method which takes HashSet argument which is sub class
    public Collection sort(HashSet hs){
        System.out.println("Inside HashSet sort method");
        return hs;
    }
     
}

Output:
Inside Collection sort method

In above example of static binding in Java we have overloaded sort() method, one of which accept Collection and other accept HashSet. we have called sort() method with HashSet as object but referenced with type Collection and when we run method with collection as argument type gets called because it was bonded on compile time based on type of variable (Static binding)  which was collection.

Example of Dynamic Binding in Java

In last section we have seen example of static binding which clears things that static binding occurs on compile time and Type information is used to resolve methods. In this section we will see example of dynamic binding in java which occurs during run time and instead of Type or Class information, Object is used to resolve method calls.

public class DynamicBindingTest {

    public static void main(String args[]) {
        Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
        vehicle.start();       //Car's start called because start() is overridden method
    }
}

class Vehicle {

    public void start() {
        System.out.println("Inside start method of Vehicle");
    }
}

class Car extends Vehicle {

    @Override
    public void start() {
        System.out.println("Inside start method of Car");
    }
}

Output:
Inside start method of Car

In this example of Dynamic Binding we have used concept of method overridingCar extends Vehicle and overrides its start() method and when we call start() method from a reference variable of type Vehicle, it doesn't call start() method from Vehicle class instead it calls start() method from Car subclass because object referenced by Vehicle type is a Car object. This resolution happens only at runtime because object only created during runtime and called dynamic binding in Java. Dynamic binding is slower than static binding because it occurs in runtime and spends some time to find out actual method to be called.

That's all on difference between static and dynamic binding in java. bottom line is static binding is a compile time operation while dynamic binding is a runtime. one uses Type and other uses Object to bind. staticprivate and final methods and variables are resolved using static binding which makes there execution fast because no time is wasted to find correct method during runtime.


Read more: http://javarevisited.blogspot.com/2012/03/what-is-static-and-dynamic-binding-in.html#ixzz2LyJRdd1O

No comments:

Post a Comment