Difference between Static and Dynamic Binding in Java

When you call a method in Java, it is resolved either at compile time or at runtime, depending upon whether it's a virtual method or a static method. When a method call is resolved at compile time, it is known as static binding, while if method invocation is resolved at runtime, it is known as Dynamic binding or Late binding. Since Java is an object-oriented programming language and by virtue of that it supports Polymorphism. Because of polymorphism, a reference variable of type Parent can hold an object of type Child, which extends Parent. Now if you call a virtual method (not private, final or static) on this object, then Compiler can not find actual method, because it could be the one, which is defined in the Parent class, or the one which Child has overridden. This call can only be resolved at runtime when the actual object is available. That's why this is known as runtime or dynamic binding.

On the other hand, private, static and final methods are resolved at compile time, because compiler knows that they can't be overridden and only possible methods are those, which are defined inside a class, whose reference variable is used to call this method.

This is known as static or compile time binding, all private, static and final methods are resolved using static binding. This concept is also closely related to method overloading and method overriding.

As dynamic binding happens when method overriding is possibility and overloaded method calls are resolved at compile time, because they are always defined in same class.

In this article, we will learn few more difference between Static and Dynamic Binding in Java, but before this let's see couple of examples of static and dynamic binding :




Static vs Dynamic Binding in Java

Following Java program will help you to understand difference between static and dynamic binding in Java. In below example, we have two class Parent and Child, where Child is sub-class of Parent. In superclass we have three methods, one private, one static and one virtual, similarly on child class we have defined all those three methods, with exact same name and syntax.

Since private and static method can not be overridden, they simply hide super class implementation. Only virtual method can be overridden and those will be resolved using dynamic binding.  Test code is written in a test class called, HelloAndroid, where a reference variable of type Parent is pointing to object of Child class.

This reference variable is then used to call static method whoAmI(), and virtual method whoAreYou(), defined on both super and sub class, and since we can not call private method from outside the class, it is  internally called inside virtual method. Now let's analyse output of first call  p.whoAmI()  , it prints "Inside static method, Parent#whoAmI()" which means static method from Parent class is invoked and object is not used in method resolution, only type is used. This is example of static binding in Java.

Difference between Static and Dynamic binding in Java
On the other hand, call to virtual method i.e. p.whoAreYou() prints two lines Child#who and Child#whoAreYou, which means virtual method from subclass is invoked, which also invoked private method from sub-class, not from super class, because its not accessible. This is an example of dynamic binding in Java, and it uses actual object for method resolution.

/**
 * 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 8
 *
 */
public class HelloAndroid {

 public static void main(String args[]) {
  
  Parent p = new Child();  
  
  p.whoAmI(); // static method, resolved at compile time
  p.whoAreYou(); // virtual method, runtime resolution
 }
 

}

class Parent {
 
 private void who(){
  System.out.println("Inside private method Parent#who()");
 }
 
 public static void whoAmI(){
  System.out.println("Inside static method, Parent#whoAmI()");
 }
 
 public void whoAreYou(){
  who();
  System.out.println("Inside virtual method, Parent#whoAreYou()");
 }
}
class Child extends Parent{ 

 private void who(){
  System.out.println("Child#who()");
 }
 
 
 public static void whoAmI(){
  System.out.println("Child#whoAmI()");
 }
 
 @Override
 public void whoAreYou(){
  who();
  System.out.println("Child#whoAreYou()");
 }
}

Output:
Inside static method, Parent#whoAmI()
Child#who()
Child#whoAreYou()

In short following are key differences between Static and Dynamic binding in Java :

1) Static binding is resolved at compile time, while Dynamic binding is resolved at runtime.

2) Static binding only uses Type information, and method resolution is based upon type of reference variable, while dynamic or late binding resolves method based upon actual object.

3) In Java programming language, private, static and final method are resolved using static binding, while only virtual methods are resolved using dynamic binding.

4) True Polymorphism is achieved using dynamic binding, and its key of many design principles e.g. Strategy pattern, Open closed design pattern etc.

That's all about difference between static and dynamic binding in Java. You can play around this code to try different combination of method calling e.g. calling a sub class method using Super class object or vice-versa. Let me know if you have question related to understanding static or dynamic binding in Java.


1 comment:

  1. Everything in this blog post is false. You are only semantically right, kind of. Let me quote the latest (Java SE 8) Virtual Machine specification (page 367):

    "This specification allows an implementation flexibility as to when linking activities
    (and, because of recursion, loading) take place[..]."

    "For example, a Java Virtual Machine implementation may choose to resolve each
    symbolic reference in a class or interface individually when it is used ('lazy'
    or 'late' resolution), or to resolve them all at once when the class is being
    verified ('eager' or 'static' resolution). This means that the resolution process may
    continue, in some implementations, after a class or interface has been initialized."

    ReplyDelete