Thursday 24 January 2019

Static Binding Vs Dynamic Binding in Java

1. Objective

In this Java tutorial, we will learn Java static binding vs dynamic binding in detail. Also, we will discuss the feature wise difference between static binding and dynamic binding in Java Programming Language. Moreover, we will go through the static binding definition and definition of a dynamic binding in Java. Along with this, we will see an example of static binding vs dynamic binding in Java.
So, let’s start a comparison of Static Binding vs Dynamic Binding in Java.

Static Binding Vs Dynamic Binding in Java

2. Static Binding Vs Dynamic Binding in Java

  • Static Binding in Java
In this static binding, the binding can resolve at runtime or compile time.
The binding process of all static, final and private methods is done at compile time.
  • Dynamic binding in Java 
Overriding in Java can be considered as the best example of dynamic binding as both parent and child class have the same method, that is it doesn’t decide the method to be called.

3. What is Static Binding in Java?

Binding of static, final and private methods is always a static binding because a static binding gives better performance and they cannot be overridden and hence will always be accessed by the object of some local class.
Example –
  1. public class NewClass
  2. {
  3. public static class superclass
  4. {
  5. static void print()
  6. {
  7. System.out.println("print in superclass.");
  8. }
  9. }
  10. public static class subclass extends superclass
  11. {
  12. static void print()
  13. {
  14. System.out.println("print in subclass.");
  15. }
  16. }
  17. public static void main(String[] args)
  18. {
  19. superclass A = new superclass();
  20. superclass B = new subclass();
  21. A.print();
  22. B.print();
  23. }
  24. }
Output –
Print in superclass.
Print in superclass.
In the example above both cases give the same result because –
  • The reference here for subclass and superclass is same, that is a single object.
  • Since it cannot be overridden in a subclass, i.e. the superclass and compiler know it and so there is no ambiguity.

4. What is Dynamic Binding in Java?

Example of dynamic binding –
  1. public class NewClass
  2. {
  3. public static class superclass
  4. {
  5. void print()
  6. {
  7. System.out.println("print in superclass.");
  8. }
  9. }
  10. public static class subclass extends superclass
  11. {
  12. @Override
  13. void print()
  14. {
  15. System.out.println("print in subclass.");
  16. }
  17. }
  18. public static void main(String[] args)
  19. {
  20. superclass A = new superclass();
  21. superclass B = new subclass();
  22. A.print();
  23. B.print();
  24. }
  25. }
Output –
Print in superclass.
Print in subclass.
The output differs because –
  • Methods here are not static.
  • Amid Binding, the compiler has no clue as to which print must be called since compiler goes just by referencing variable not by kind of object and along these lines the binding would be deferred to runtime and in this way the comparing adaptation of print will be called in view of a sort on the question.

5. Essential Points for Static Binding vs Dynamic Binding

Here, we will discuss some important points related to Static vs Dynamic Binding in Java:
  • Private, last and static individuals utilize static authoritative while for virtual techniques restricting finish amid runtime in view of runtime object.
  • Static binding in Java uses type data for official while Dynamic binding in Java restricting uses objects to determine to bind.
  • Overloaded methods are settled utilizing static binding while overridden methods utilizing dynamic binding, i.e, at runtime.
So, this was all about Static Binding vs Dynamic Binding in Java. Hope you like our explanation.

No comments:

Post a Comment

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...