web analytics
Google + RSS Feed

Difference between Static and Dynamic binding in Java

2

May 7, 2014 by admin

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 overriding. Car 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 objectonly 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. static, private 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#ixzz3115WawKM


2 comments »

  1. admin says:

    Thank you ! ..

  2. Connecting the function call to the function implementation before execution is static binding whereas connection them at the run-time is dynamic binding.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 + = eleven

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts