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 Child extends Base
public static void main(String a)
Base b=new Child();
//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).
- Top 25+ Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021