The process of connecting a method definition to a method call is called binding. Binding processes are classified into two types namely:
- Static Binding
- Dynamic Binding
Static Binding
Static Binding is also known as Early Binding. If a compiler can resolve the binding process during compile time it is called as static binding. Instance methods are always resolved during run time while static methods are resolved during compile time. So static binding is used for static methods. Static methods are called as class methods and can be accessed by utilizing the class name. It is recommended to call static methods using class names. The access to the static method is required to be resolved during compile time. This is why static methods are not allowed to be overridden in Java. In Java, access to all member variables will be followed using static binding because Java discourages polymorphism.
Let us consider the following program as an example:
class SupClass
{
…
public String somVariable = “SomVariable in SupClass”;
…
}
class SuClass extends SupClass{
…
public String somVariable=”Few Variable in a Sub Class”;
…
}
…
…
SupClass supClass1 = new SupClass();
SupClass supClass2 = new SuClass();
System.out.println(supClass1.somVariable);
System.out.println(supClass2.somVariable);
…
The output for the above program is
Some Variable in SupClass
In the above program, the declared type of object reference plays an important role in resolving the member variable. The compiler is efficient enough to distinguish the type of object reference very early so it results in static binding. The methods which are declared as private will never be inherited so only compiler can resolve the method during compile time which also results in static binding.
Dynamic Binding
Dynamic Binding are also known as late binding. If a binding process is not allowed to be resolved during compile time, it is called as dynamic or late binding. Method overriding is an example for dynamic binding. In method overriding, the method of both parent and child classes are similar. The compiler gets dazed because both the methods have same name so dynamic binding will occur in method overriding.
The following program is an example for dynamic binding
class Animl
{
void eat()
{
System.out.println(“An Animal is Sleeping…”);}
}
class Dogy extends Anima
{
void eat()
{
System.out.println(” My dog is Walking…”);}
public static void main(String args[])
{
Anima a1=new Dogy();
a1.eaty();
}
}
The output for the following program is “dog is eating’.
In the program given above, the compiler cannot identify the object type. In the program instance of the dog is also an instance of an animal so the compiler gets confused. This confusion by compiler prevents the static binding during compile time. Since there is no static binding occurred during compile time, dynamic binding will occur during runtime.
- More: Java interview Q&A available here
Difference between Static and Dynamic Binding:
- Static Binding occur during compile time while dynamic binding occur during runtime.
- Methods like static, private and final are binded using static binding during compile time itself because overriding of these methods are not allowed.
- All overridden methods are binded using dynamic binding.
- Overloaded methods are binded using static binding.
The following program will clearly explain the difference between the static and dynamic binding.
/**
* Java program to show difference between static and dynamic binding in Java.
* Static method are resolved at compile time, by Type of reference variable,
* while Virtual methods are resolved at runtime, depending upon actual object.
*
* @author WINDOWS 10
*
*/
public class HelloAndroid
{
public static void main(String args[])
{
Paren p1= new Chid();
p1.whoAmI(); // static method, resolved at compile time
p1.whoAreYou(); // virtual method, runtime resolution
}
}
class Parent
{
private void who()
{
System.out.println(“Inside private method Parent #who()”);
}
public static void whAmI()
{
System.out.println(“An Inside static method, Parent #whAmI()”);
}
public void whoAreU(){
who();
System.out.println(“Inside virtual method, Paren #whoAreU()”);
}
}
class Child extends Paren
{
private void who()
{
System.out.println(“For Child… #who()”);
}
public static void whAmI()
{
System.out.println(“Child #whAmI()”);
}
@Override
public void whoAreU(){
who();
System.out.println( “Child #whAreU()”);
}
}
Output:
Inside static method, Paren#whAmI()
Child#who()
Child#whoAreU()
In the above program, there are two classes namely parent and child. The class child is the subclass of parent. The parent superclass have three methods. Each of these methods belong to static, private and virtual type of methods. In child class all the methods were defined under same name and syntax. The implementation of superclass is hidden because private and static methods cannot be overridden. Virtual method can be overridden and will be resolved using dynamic binding while private and static methods are resolved using static binding.