Dynamic Binding

Java gave us many important concepts like Inheritance, which is used to create a hierarchy of classes. And while coding with inheritance, we encountered another concept which we call as Method Overriding. In method overriding, we have a method in the sub-class as well as in the super-class, both having the same name and return type. While overriding a method the problem was that, how the compiler would know which function is being referred, by the caller. Therefore, to deal with this, java gave us Dynamic Binding.

Dynamic Binding refers to the concept where the compiler is unable to resolve the call to exact function, and binding is done at run-time.

Before understanding what dynamic binding is, you should first know that an object is divided into two parts; the first is known as reference variable while the second is called the reference identity. Suppose we have an object of class Obj;

Obj o= new Obj ();

in which o is the reference variable, that takes memory in stack; while new Obj () is it’s reference identity. The reference identity of an object can be stored in any number of reference variables.

Java took the advantage of the feature to store the reference identity of an object in any number of reference variables and solved the problem of calling an overridden method at run-time.

Dynamic binding often referred as dynamic method dispatch or dynamic method binding is the mechanism by which a call to an overridden method is resolved at run-time rather than at compile time.

Let us consider an example;
class Base
{
void show()
{
System.out.prinln(“Base”);
}
}
class Child extends Base
{
void show()
{
System.out.println(“Child”);
}
void display()
{
System.out.println(“Display”);
}
public static void main(String a[])
{
Base b=new Child();
b.show();
//b.display(); it gives an error
}
}

In the above example, we can see that in the main method b is the reference variable of Base class, but it contains the reference identity of Child class. Hence, using this object when we call show method, it refers to show () of Child class. While if we try to call display () with the same object, then it gives us an error. By creating an object of Child class, and with the help of this object we can call display ().

On the other hand, if we want to call the show () of Base class, we have to fulfil either of the two conditions;
1) Create an object of Base class holding reference identity of Base class, or
2) Avoid using method overriding.
Well, according to me first option will be better.

At this time you may not find dynamic binding of much use. But in cases where the level of the class hierarchy is much higher, dynamic binding is found of much use. You’ll be finding it more beneficial while coding for some real life java applications.

At a higher level of class hierarchy, we may have more than one overridden method. Using dynamic binding in such cases different types of objects refer to different versions of an overridden method at run-time. In other words, it is the type of the object being referred to (not the type of reference variable) that determines which version of an overridden method will be executed.

Using Dynamic Binding java implements run-time polymorphism (a crucial nerve in java’s body).

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.
Scroll to Top