In order to understand the difference between static and dynamic binding in Java, it's important to first learn what is binding? Binding means the link between reference and actual code e.g. when you refer a variable it's bonded to the code where it is defined, similarly when you call a method, it's linked to the code where a method is defined. There are two types of method binding in Java, static binding and dynamic binding. When a method is called in Java it's bonded to the actual code either at compile time or runtime, when the program is actually started and objects are created.
As the name suggests, static binding is more of static nature hence it occurs at compile time i.e. your code knows which method to call once you compiled your Java source file into a class file. Since it happens early in the program's life cycle it is also known as early binding in Java.
On the other hand, dynamic binding occurs at runtime, when JVM starts your program. This time which method to call is figured out by an actual object, which information was not available at compile time because objects are created at runtime. Since it happens late in the program life cycle, it is also known as late binding in Java.
So, this was the fundamental difference between static binding vs dynamic binding in Java, one occurs early at compile time using the type of reference variable, while the other occurs late at runtime by using actual objects. Let's see a couple of more differences to understand this concept better. It is also one of the frequently asked Java questions, especially during the first few rounds, so spending time here will be definitely worth it.
On the other hand, dynamic binding occurs at runtime, when JVM starts your program. This time which method to call is figured out by an actual object, which information was not available at compile time because objects are created at runtime. Since it happens late in the program life cycle, it is also known as late binding in Java.
So, this was the fundamental difference between static binding vs dynamic binding in Java, one occurs early at compile time using the type of reference variable, while the other occurs late at runtime by using actual objects. Let's see a couple of more differences to understand this concept better. It is also one of the frequently asked Java questions, especially during the first few rounds, so spending time here will be definitely worth it.
Static vs Dynamic Binding Java
There are many points on which you can differentiate static binding and dynamic binding, but the most important is how it is used by JVM. Have you ever thought about how JVM decides which methods to call if there is more than one method in the scope with the same name?If you have ever used method overloading or method overriding then you know that it's possible in Java to have multiple methods with the same name. JVM uses both static and dynamic binding to find the right method in Java.
Static and binding example in Java
In this program, you will see that virtual methods are not bonded at compile time using static binding because if that happens then the method from superclass would have been called as is the case of the static method which is bonded early.Since the method from the subclass is called it's proved that the actual object is used for function binding at runtime, hence dynamic binding is used to bind virtual methods.
You can see here that the call to premium() method executed the method from subclass while the call to category() method is executed from the superclass.
public class Main { public static void main(String[] args) { // An example of static and dynamic binding in Java Insurance current = new CarInsurance(); // dynamic binding based upon object int premium = current.premium(); // static binding based upon class String category = current.category(); System.out.println("premium : " + premium); System.out.println("category : " + category); } } class Insurance{ public static final int LOW = 100; public int premium(){ return LOW; } public static String category(){ return "Insurance"; } } class CarInsurance extends Insurance{ public static final int HIGH = 200; public int premium(){ return HIGH; } public static String category(){ return "Car Insurance"; } } Output premium : 200 category : Insurance
You can see here that the call to premium() method executed the method from subclass while the call to category() method is executed from the superclass.
This happens because premium() is a virtual method and resolved using late binding while category() is a static method and resolved using static binding at compile time using the class name.
You can further check out these free Java courses from Udemy to learn more about the difference between the static and non-static methods in Java.
1) The static binding occurs at compile time while dynamic binding happens at runtime.
2) Since static binding happens at an early stage of the program's life cycle, it also is known as early binding. Similarly, dynamic binding is also known as late binding because it happens late when a program is actually running.
3) Static binding is used to resolve overloaded methods in Java, while the dynamic binding is used to resolve overridden methods in Java.
4) Similarly, private, static, and final methods are resolved by static bonding because they can't be overridden and all virtual methods are resolved using dynamic binding.
5) The actual object is not used in the case of Static binding, instead, the type information i.e. the type of reference variable is used to locate the method. On the other hand, dynamic binding uses an actual object to find the right method in Java.
Here is a nice exercise that is based on the concept of static and dynamic binding in Java, let's see if you can answer this question:
Let us know what does this program will print? Whether it will print Collection, Set, or HashSet?
That's all about the difference between early (static) and late (dynamic) binding in Java. It's one of the best Java phone interview questions as it offers several opportunities to test the depth of a candidate's Java knowledge.
Difference between early and late binding in Java
Now that you know and understand how method calls are bonded in Java and how static and dynamic binding works, let's revise some important, key differences between static and dynamic binding in Java:1) The static binding occurs at compile time while dynamic binding happens at runtime.
2) Since static binding happens at an early stage of the program's life cycle, it also is known as early binding. Similarly, dynamic binding is also known as late binding because it happens late when a program is actually running.
3) Static binding is used to resolve overloaded methods in Java, while the dynamic binding is used to resolve overridden methods in Java.
4) Similarly, private, static, and final methods are resolved by static bonding because they can't be overridden and all virtual methods are resolved using dynamic binding.
5) The actual object is not used in the case of Static binding, instead, the type information i.e. the type of reference variable is used to locate the method. On the other hand, dynamic binding uses an actual object to find the right method in Java.
Here is a nice exercise that is based on the concept of static and dynamic binding in Java, let's see if you can answer this question:
Let us know what does this program will print? Whether it will print Collection, Set, or HashSet?
That's all about the difference between early (static) and late (dynamic) binding in Java. It's one of the best Java phone interview questions as it offers several opportunities to test the depth of a candidate's Java knowledge.
Always remember that private, static, and final methods are bonded using static binding and virtual methods are bonded using dynamic binding.
Similarly, method overloading is the best example of static binding, and method overriding is the example of dynamic binding.
Other interesting Java Interview questions you may like
Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
Other interesting Java Interview questions you may like
- Difference between ArrayList and LinkedList in Java? (answer)
- Difference between Overloading and Overriding in Java? (answer)
- Difference between multithreading and multi-tasking in Java? (answer)
- What is the difference between Abstract class and interface in Java? (answer)
- Difference between URL and URI in Java? (answer)
- Difference between JIT and JVM in Java? (answer)
- Difference between Abstraction and Encapsulation in Java? (answer)
- Difference between static and non-static nested classes in Java? (answer)
Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
And lastly one question for you? What is SOLID design principle? Can you name all the 5 object oriented principle which are part of SOLID?
It will print Collection in the above asked example
ReplyDeleteYes, it will. But can you brief me why it will happen. I'm unable to understand that.
DeleteThe trick resides in this line;
DeleteCollection c = new HashSet();
Subclass-Superclass binding is done at runtime, and since "print" is a static method, it would take SuperClass as a parameter.