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.
programmerravi
source share