Java Made Clear: Difference Static binding Dynamic binding

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).

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.

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.

One thought on “Java Made Clear: Difference Static binding Dynamic binding

Leave a Reply

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