JavaRush /Java Blog /Random EN /Differences between early and late binding in Java

Differences between early and late binding in Java

Published in the Random EN group
To understand the difference between early (static) and late (dynamic) binding in Java, you must first understand what binding is . Linking means there is a connection between the link and the code. For example, a variable you reference is bound to the code in which it is defined. Likewise, the method being called is bound to the location in the code where it is defined.
Differences between early and late binding in Java - 1
There are two types of method binding in the Java language: early binding (also called static) and late binding (respectively, dynamic) . Calling a method in Java means that the method is bound to specific code, either at compile time or at run time, when the program runs and objects are created. As the name suggests, static linking is more static in nature as it happens at compile time, meaning the code “knows” which method to call after compiling the Java source code into class files. And since this refers to the early stage of the program's life cycle, it is also called early binding. On the other hand, dynamic linking occurs at runtime, after the program is run by the Java Virtual Machine. In this case, which method to call is determined by the specific object, so the information is not available at compile time because objects are created at run time. And since this happens late in the program's life cycle, it is called late binding in Java.
So, the fundamental difference between static and dynamic binding in Java is that the former occurs early, at compile time, based on the type of the reference variable, and the latter occurs later, at runtime, using concrete objects.
Let's look at a few more differences to understand this better and also be able to answer this very popular question asked in Java interviews.

Early and Late Binding in Java

There are many differences between static and dynamic binding in Java, but the most important is how the JVM uses them. Have you ever wondered how the JVM decides which method to call when there is more than one method with the same name in scope? If you've ever used method overloading or overriding, you know that in Java you can have multiple methods with the same name. In the case of Java, the JVM uses both static and dynamic binding to select the desired method.

Example of static and dynamic binding in Java

In this program, you will see that binding of virtual methods does not occur at compile time using static binding, since this would call a method from the superclass, as happens with static methods that are bound early. If a method from a subclass is called, a specific object was used to bind the function at runtime, and hence dynamic binding is used to bind virtual functions.
public class Main {
  public static void main(String[] args) {

    // Пример статического и динамического связывания в Java
    Insurance current = new CarInsurance();

    // Динамическое связывание на основе an object
    int premium = current.premium();

    // Статическое связывание на основе класса
    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";
  }

}
Результаты выполнения:

premium : 200
category : Insurance
As you can see, a method call premium()resulted in the execution of a method from the subclass, while a method call category()resulted in the execution of a superclass method. This is because premium()- is a virtual method, which is resolved using late binding, while category()- is a static method, which is resolved using compile-time static binding by class name.
Interested in reading about Java? Join the Java Developer group !

Differences between early and late binding in Java

Now that you have a handle on how Java binds method calls and how static and dynamic binding works, let's recap the key differences between early and late binding in Java:
  1. Static linking occurs at compile time, while dynamic linking occurs at run time.

  2. Because static linking occurs early in a program's life cycle, it is called early binding. Similarly, dynamic binding is also called late binding because it occurs later in the program's execution.

  3. Static binding is used in Java language to resolve overloaded methods while dynamic binding is used in Java language to resolve overridden methods.

  4. Likewise, private, static, and terminal methods are resolved using static binding because they cannot be overridden, while all virtual methods are resolved using dynamic binding.

  5. In the case of static binding, it is not concrete objects that are used, but type information, that is, the type of the reference variable is used to discover the desired method. On the other hand, dynamic binding uses a specific object to find the desired method in Java.
Here's a good exercise based on the concepts of static and dynamic binding in Java. Can you answer the question: "What will be output when the following program is executed?"
Differences between early and late binding in Java - 2
What will this program output? Collection, Setor HashSet? That's all we wanted to tell you about the differences between early (static) and late (dynamic) binding in Java. This is one of the best Java phone interview questions because it provides quite a few opportunities to test the candidate's depth of knowledge. Always remember that private , static , and final methods are bound using static linking , while virtual methods are bound using dynamic linking . Likewise, the best example of static binding is method overloading, while overriding is dynamic. Source
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION