Java Made Clear: Difference Static binding Dynamic binding


Difference Static binding Dynamic binding

Java, being an OOPs language, supports both static binding and dynamic binding. Coming to our topic, a class may have overloaded and overridden methods. When to call which method is decided (binded) sometimes by compiler and sometimes by JVM at runtime. Why this disparity? Why Java is designed like this? All the questions will be answered right now.

A) Static binding with Method Overloading

Instead of writing hundred lines of narration, it is better to explain the concept through a program (like, instead of writing 100 lines about Charminar monument, better show an photo of Charminar; child gets clear idea fast).

public class Area
{
  public void calculate()		         // I
  {
    System.out.println("Nothing to calculate");
  }
  public void calculate(int x)		         // II
  {
    System.out.println("Circle Area: " + Math.PI * Math.pow(x, 2));
  }
  public void calculate(double x)		 // III
  {
    System.out.println("Circle Perimeter: " + 2 * Math.PI * x);
  }
  public void calculate(int x, int y)	         // IV
  {
    System.out.println("Rectangle Area: " + x*y);
  }
  public static void main(String args[])
  {
    Area a1 = new Area();
    a1.calculate();		    // I
    a1.calculate(10, 20);	    // IV
    a1.calculate(10.5);		    // III
    a1.calculate(10);		    // II
  }
}

Difference Static binding Dynamic binding

In the above code, calculate() method is overloaded (known as Method Overloading) 4 times. In the main() method all the 4 methods are called. Okay, nice. When a1.calculate(10.5) is called, now which overloaded method to be called is decided at compile time or runtime. Which is better you think, say you are developing a language. Any Designer’s prime importance is performance. If decided at compile time, at run time (or execution time), the method is simply called without wasting time in thinking. That is, decided at compile time and called (or executed) at runtime.

Observe, the compiler has got all the information to bind the method at compile time. In a1.calculate(10.5), the parameter is double value. Compiler searches for a double value parameter calculate() method and binds it. This increases performance of execution. Deciding earlier at compile time is known as compile time binidng or static binding.

Static binding is achieved through method overloading. Static binding increases performance.

Note: Static binding cannot be done with private, final and static methods.

B) Dynamic binding with Method Overriding

Dynamic binding is achieved through method overriding. Let us write a program with method overriding.

class Lecturer
{
  public void call()		// I
  {
    System.out.println("Hello Sir, Good morning");
  }
}
class Raju extends Lecturer
{
  public void call()		// II
  {
    System.out.println("Hello Raju, improve your programming skills");
  }
}

class Prasad extends Lecturer
{
  public void call()		// III
  {
    System.out.println("Hello Prasad, improve your English language");
  }
}

class Jyostna extends Lecturer
{
  public void call()		// IV
  {
    System.out.println("Hello Jyostna, improve your aptitude and reasoning skills");
  }
}

public class DynamicBindingExample
{
  public static void main(String args[])
  {
    Lecturer l1 = new Lecturer();			l1.call();	// I
    Raju r1 = new Raju();	   l1 = r1;		l1.call();	// II
    Prasad p1 = new Prasad();	   l1 = p1;		l1.call();	// III
    Jyostna j1 = new Jyostna();	   l1 = j1;		l1.call();	// IV
  }
}

Difference Static binding Dynamic binding

There are three subclasses (Hierarchical inheritance), Raju, Prasad and Jyostna for Lecturer class. All the three has overridden the call() method.

l1 = r1;
l1.call();

In the main() method, the super class object l1 is assigned with subclass object Raju r1. Now l1 will call which call() method, its own or subclass Raju’s; the doubt came because in l1, the reference of subclass object Raju exists. Definitely you say l1 calls r1 call() method.

In the next statement the reference r1 is replaced by p1, the subclass object of Prasad. Now l1 will call Prasad call() method. Similarly, in the immediate next statement, p1 reference is replaced by j1, the Jyostna object. Now l1 will call Jyostna’s call() method.

It is okay. The question is, replacement of subclass objects r1, p1 and j1 in the super class object happens at compile time or runtime. This replacement will be done at runtime as the execution proceeds forwards statement by statement. When replaced, l1 calls first Raju’s call() method and later that of Prasad and Jyostna. Which method is to be called is decided at runtime and is known as runtime binding or dynamic binding.

Dynamic binding is achieved through method overriding. If method overriding does not exist, the above program does not work to call subclass methods.

The next of step of learning is Static polymorphism and Dynamic polymorphism as Static binding and dynamic binding leads to Static polymorphism and Dynamic polymorphism.

1 thought on “Java Made Clear: Difference Static binding Dynamic binding”

Leave a Comment

Your email address will not be published.