Java late binding

I looked through all the similar questions about late binding when stack overflowed, and I would strongly disagree with whoever marks this issue as a duplicate. Firstly, I found this example on another issue, but I donโ€™t understand how I should know when something is solved at compile time and when something is solved at runtime. In fact, the essence of my question comes down to two things:

  • Which in this example should lead me to the logical conclusion that one method is late binding and the other is early binding

  • How to find out when the decision about which version of a method to execute is determined at runtime or compile time in Java

The code:

class A { public void foo() { System.out.println("Class A"); } } class B extends A { public void foo() { System.out.println("Class B"); } } public class C { public static void main(String [] args) { A a=new A(); B b=new B(); A ref=null; /* early binding --- calls method foo() of class A and decided at compile time */ a.foo(); /* early binding --- calls method foo() of class B and decided at compile time */ b.foo(); /* late binding --- --- calls method foo() of class B and decided at Run time */ ref=b; ref.foo(); } } 
+8
java late-binding
source share
5 answers

Wrong on all counts. The method that will be called is determined at runtime, in each case here, depending on the type of runtime of the object. The only decisions made during compilation are calls for final, private, or static methods, or a choice between many overloaded methods (which can still lead to runtime choices if the overloaded methods are not final, private, or static). A.

+6
source share

Java uses late binding for all non-financial, non-private instance methods. So polymorphism is realized. All the calls you commented out are determined at runtime.

IN

 A a=new A(); a.foo(); 

a refers to the object a , so the implementation of a will be found, bound, and used.

IN

 B b=new B(); b.foo(); 

b refers to the object b , so b implementation will be found, bound and used.

IN

 ref=b; ref.foo(); 

ref refers to object b , so implementation b will be found, bound, and used.

The static (declared) type of a variable is used only by the compiler to make sure that such a method is available for this type.

on this topic:

+2
source share

All of the answers here are mostly correct, but there is no one key point related to late binding in Java.
Java doesnโ€™t do late-binding โ€œby the bookโ€ if we move on to the definition of late binding. The later binding in his book definition form means that the compiler should not perform any argument checks, no type checks in the method call, and should not leave all this at run time - since the compiler may not have access to the method implementation code (for example, COM programming). However, Java at compile time, even in a polymorphic script, checks that the called method and method signature exist somewhere in the type hierarchy of the expression that qualifies the method. For example, let's say I call the foo1 method in ref, which does not exist in either A or B:

 A ref=null; ref=new B(); ref.foo1(); //This will not compile in Java, because java will check at compile time //for the method foo1 in the type hierarchy of A, which is the type of the // variable ref at compile time. //In pure late binding, however this would pass compilation and //throw an error at runtime. 

In a pure late binding scenario, determining whether the foo1 () method exists or not with the correct number of arguments is done exclusively at run time. However, in Java, some level of verification is performed at compile time to ensure that a method with the correct number of arguments exists somewhere in the type hierarchy.
The only time I think Java performs pure late binding is to use a reflex to invoke a method. What Java does is best called dynamic dispatch, not late binding, but everyone calls it late binding in Java and therefore there is confusion.

+1
source share

Consider the statement

  A ref; //reference of A ref = new B();//Object of B ref.f2(); 

Here, ref is a class A reference and has an object address of class B f2 () - this is the overridden method.

When the compiler detects such a statement, it does not associate the function call with any definition. It only checks the call.

Linking such calls remains to the runtime. During program execution, the system identifies the data type of the object and associates the function call with the function definition provided by the object class. This type of binding between a function call and a function definition is called "late binding" or "run-time binding" or "run-time polymorphism" or "dynamic method manager".

go through this question and read my answer , an example is also given here.

0
source share

When you call a method on any object, always remember that the "least modified version" of the method will be called in the inheritance. This is nothing but the dynamic selection of a version of a method from a hierarchy.

0
source share

All Articles