Late binding of methods is when there is a reference variable, and depending on which class instance will be created, the corresponding method will be called. What about early binding what is the difference?

    2 answers 2

    Early binding is when the method to be invoked is known at compile time, for example, calling a static method.

    By the way, what you call late binding is rather dynamic dispatch.

    Late binding is when a method call can be made only at run time and the compiler does not have the information to check the correctness of such a call. In java, this can be done with the help of reflection.

    • Possible example of late binding - JAVAvladuxa
    • five
      Here is an example: Object a = ... // some assignment a.toString (); At the compilation stage, we do not know what type of object is a . It can be either Object itself or any of its descendants, in which the toString() method is overridden. It is at the execution stage that the type a is determined and toString() is called from the class of which type a . This is late binding. - fori1ton
    • @JAVAVladuxa, see en.wikipedia.org/wiki/Late_binding#Late_binding_in_Java - misha-nesterenko

    Early binding, as noted above, occurs at compile time. It is used when calling ordinary methods (not virtual).

    Late binding on the contrary occurs at run time. It is executed when calling the virtual functions of the descendant class to determine which method should be called.

    Based on the fact that early binding is performed at the compilation stage, and later - in runtime, the first option has the best speed, but the second is necessary for the implementation of polymorphism.

    Regarding Java, I can say that there, if I am not mistaken, late binding is applied to all methods by default (if they are not marked with the final modifier), unlike, say, C ++, where early binding is applied by default. To further understand the issue, read about the virtual methods table.

    ZY I'm not familiar with Java, so I can’t say exactly how applicable the term "function" is

    • > Early binding, as noted above, occurs at compile time. It is used when calling ordinary methods (not virtual). if the compiler clearly sees which method will be invoked, even if the method is “under the strongest polymorphism”, then no one bothers the compiler to make early binding. But in java it is even more blurred. jit can figure out which method is being invoked and generate code equivalent to early binding. - KoVadim