Late binding
Encyclopedia
Late binding is a computer programming mechanism in which the method being called upon an object is looked up by name at runtime. This is informally known as duck typing or name binding.
Late binding is often confused with dynamic dispatch
, but there are significant differences. With early binding the compiler statically verifies that there are one or more methods with the appropriate method name and signature. This is usually stored in the compiled program as an offer in a virtual method table
("v-table") and is very efficient. With late binding the compiler does not have enough information to verify the method even exists, let alone bind to its particular slot on the v-table. Instead the method is looked up by name at runtime.
The primary advantage of using late binding in Component Object Model
(COM) programming is that it does not require the compiler to reference the libraries that contain the object at compile time. This makes the compilation process more resistant to version conflicts, in which the class's v-table may be accidentally modified. (This is not a concern in JIT
-compiled platforms such as .NET or Java, because the v-table is created by the secondary compiler against the libraries as they are being loaded into the running application.)
Late binding necessarily prevents the use of static type checking. When making a late bound call, the compiler has to assume that the method exists. This means a simple spelling error can cause a runtime error to be thrown. The exact exception varies by language, but it is usually named something like "Method Not Found" or "Method Missing".
Late binding prevents many forms of static analysis needed by an integrated development environment
(IDE). For example, an IDE's "go to definition" feature cannot be used on a late-bound call, because the IDE has no way to know which class the call may refer to. Another problem is that the lack of typing information prevents the creation of dependency graphs. However, other programming methods such as abstract interfaces can result in the same problems.
. The term was widely used to describe languages such as LISP, though usually with negative connotations about performance.
In the 1980s Smalltalk
popularized object-oriented programming
(OOP) and with it late binding. Dr. Alan Kay
once said, "OOP to me means only messaging, local retention, and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them."
In the early to mid-1990s, Microsoft heavily promoted its COM standard as a binary interface between different OOP programming languages. COM programming equally promoted early and late binding, with many languages supporting both at the syntax level.
In 2000, Alex Martelli
coined the term "duck typing
" to refer to the same concept, but with a different emphasis. While late binding generally focuses on implementation details, duck typing focuses on the ability to ignore types and concentrate on the methods an object currently has.
interface. Some COM-based languages such as Visual Basic 6 have syntactical support for calling this interface. This is done by defining the variable's type as Object. Others such as C++ require that you explicitly call GetIDsOfNames to look up a method and Invoke to call it.
Prior to version 4, C# only allowed late binding via the appropriate reflection API. A different API would be needed for each of .NET, COM, and DLR objects. With C# 4, the language gained the "dynamic" pseudo-type. This would be used in place of the Object type to indicate that late binding is desired. The specific late binding mechanism needed is determined at runtime using the Dynamic Language Runtime as a starting point.
Visual Basic uses them whenever the variable is of type Object and the compiler directive "Option Strict Off" is in force. This is the default setting for a new VB project. Prior to version 9, only .NET and COM objects could be late bound. With VB 10, this has been extended to DLR-based objects.
Early documents on Java discussed how classes were not linked together at compile time. While types are statically checked at compile time, different implementations for classes could be swapped out just prior to runtime simply by overwriting the class file. As long as the new class definition had the same class and method names, the code would still work. In this sense it is similar to the traditional definition of late binding.
Currently, it is popular to use the term late binding in Java programming as a synonym for dynamic dispatch
. Specifically, this refers to Java's single dispatch mechanism used with virtual methods.
Finally, Java can use late binding using its reflection APIs and type introspection
much in the same way it is done in COM and .NET programming. Generally speaking those who only program in Java do not call this late binding. Likewise the use of "duck typing" techniques is frowned upon in Java programming, with abstract interfaces used instead.
It should be noted that Oracle, the current owner of Java, has been known to use the term late binding in the "duck typing" sense when discussing both Java and other languages in the same documentation.
When using late binding the timestamp check is not performed, and the stored procedure is executed via an anonymous PL/SQL block. While this can be slower, it removes the need to recompile all of the client applications when a stored procedure changes.
This distinction appears to be unique to PL/SQL and Ada. Other languages that can call PL/SQL procedures, as well as other database engines, only use late binding.
Late binding is often confused with dynamic dispatch
Dynamic dispatch
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code at runtime. This is done to support the cases where the appropriate method can't be determined at compile-time...
, but there are significant differences. With early binding the compiler statically verifies that there are one or more methods with the appropriate method name and signature. This is usually stored in the compiled program as an offer in a virtual method table
Virtual method table
A virtual method table, virtual function table, dispatch table, or vtable, is a mechanism used in a programming language to support dynamic dispatch ....
("v-table") and is very efficient. With late binding the compiler does not have enough information to verify the method even exists, let alone bind to its particular slot on the v-table. Instead the method is looked up by name at runtime.
The primary advantage of using late binding in Component Object Model
Component Object Model
Component Object Model is a binary-interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages...
(COM) programming is that it does not require the compiler to reference the libraries that contain the object at compile time. This makes the compilation process more resistant to version conflicts, in which the class's v-table may be accidentally modified. (This is not a concern in JIT
Just-in-time compilation
In computing, just-in-time compilation , also known as dynamic translation, is a method to improve the runtime performance of computer programs. Historically, computer programs had two modes of runtime operation, either interpreted or static compilation...
-compiled platforms such as .NET or Java, because the v-table is created by the secondary compiler against the libraries as they are being loaded into the running application.)
Criticism
Late binding has poorer performance than an early bound method call. Under most implementations the correct method address must be looked up by name with each call, requiring relatively expensive dictionary search and possibly overload resolution logic.Late binding necessarily prevents the use of static type checking. When making a late bound call, the compiler has to assume that the method exists. This means a simple spelling error can cause a runtime error to be thrown. The exact exception varies by language, but it is usually named something like "Method Not Found" or "Method Missing".
Late binding prevents many forms of static analysis needed by an integrated development environment
Integrated development environment
An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development...
(IDE). For example, an IDE's "go to definition" feature cannot be used on a late-bound call, because the IDE has no way to know which class the call may refer to. Another problem is that the lack of typing information prevents the creation of dependency graphs. However, other programming methods such as abstract interfaces can result in the same problems.
History
The term "late binding" dates back to at least the 1960s, where it can be found in Communications of the ACMCommunications of the ACM
Communications of the ACM is the flagship monthly journal of the Association for Computing Machinery . First published in 1957, CACM is sent to all ACM members, currently numbering about 80,000. The articles are intended for readers with backgrounds in all areas of computer science and information...
. The term was widely used to describe languages such as LISP, though usually with negative connotations about performance.
In the 1980s Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
popularized object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
(OOP) and with it late binding. Dr. Alan Kay
Alan Kay
Alan Curtis Kay is an American computer scientist, known for his early pioneering work on object-oriented programming and windowing graphical user interface design, and for coining the phrase, "The best way to predict the future is to invent it."He is the president of the Viewpoints Research...
once said, "OOP to me means only messaging, local retention, and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them."
In the early to mid-1990s, Microsoft heavily promoted its COM standard as a binary interface between different OOP programming languages. COM programming equally promoted early and late binding, with many languages supporting both at the syntax level.
In 2000, Alex Martelli
Alex Martelli
Alex Martelli is an Italian computer engineer and member of the Python Software Foundation. Since early 2005, he works as "Über Tech Lead" for Google, Inc. in Mountain View, California...
coined the term "duck typing
Duck typing
In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface...
" to refer to the same concept, but with a different emphasis. While late binding generally focuses on implementation details, duck typing focuses on the ability to ignore types and concentrate on the methods an object currently has.
Late binding in dynamically-typed languages
In most dynamically-typed languages, the list of methods on an object can be altered at runtime. For this reason most or all method calls on a dynamically-typed language are done exclusively via a late binding. These languages may not even have v-tables and instead store the methods in a dictionary.Late binding in COM languages
In COM programming a late-bound method call is performed using the IDispatchIDispatch
IDispatch is the interface that exposes the OLE Automation protocol. It is one of the standard interfaces that can be exposed by COM objects. The I in IDispatch refers to interface...
interface. Some COM-based languages such as Visual Basic 6 have syntactical support for calling this interface. This is done by defining the variable's type as Object. Others such as C++ require that you explicitly call GetIDsOfNames to look up a method and Invoke to call it.
Late binding in .NET
Like COM and Java, the Common Language Runtime provides reflection APIs that make late binding calls possible. The use of these calls varies by language (C# and Visual Basic).Prior to version 4, C# only allowed late binding via the appropriate reflection API. A different API would be needed for each of .NET, COM, and DLR objects. With C# 4, the language gained the "dynamic" pseudo-type. This would be used in place of the Object type to indicate that late binding is desired. The specific late binding mechanism needed is determined at runtime using the Dynamic Language Runtime as a starting point.
Visual Basic uses them whenever the variable is of type Object and the compiler directive "Option Strict Off" is in force. This is the default setting for a new VB project. Prior to version 9, only .NET and COM objects could be late bound. With VB 10, this has been extended to DLR-based objects.
Late binding in Java
There are three definitions for late binding in Java.Early documents on Java discussed how classes were not linked together at compile time. While types are statically checked at compile time, different implementations for classes could be swapped out just prior to runtime simply by overwriting the class file. As long as the new class definition had the same class and method names, the code would still work. In this sense it is similar to the traditional definition of late binding.
Currently, it is popular to use the term late binding in Java programming as a synonym for dynamic dispatch
Dynamic dispatch
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code at runtime. This is done to support the cases where the appropriate method can't be determined at compile-time...
. Specifically, this refers to Java's single dispatch mechanism used with virtual methods.
Finally, Java can use late binding using its reflection APIs and type introspection
Type introspection
In computing, type introspection is a capability of some object-oriented programming languages to determine the type of an object at runtime. This is a notable capability of the Objective-C language, and is a common feature in any language that allows object classes to be manipulated as first-class...
much in the same way it is done in COM and .NET programming. Generally speaking those who only program in Java do not call this late binding. Likewise the use of "duck typing" techniques is frowned upon in Java programming, with abstract interfaces used instead.
It should be noted that Oracle, the current owner of Java, has been known to use the term late binding in the "duck typing" sense when discussing both Java and other languages in the same documentation.
Early vs. late binding in PL/SQL and Ada
When using early binding between Ada and a database-stored procedure, a timestamp is checked to verify that the stored procedure has not changed since the code was compiled. This allows for faster executions and prevents the application from running against the wrong version of a stored procedure.When using late binding the timestamp check is not performed, and the stored procedure is executed via an anonymous PL/SQL block. While this can be slower, it removes the need to recompile all of the client applications when a stored procedure changes.
This distinction appears to be unique to PL/SQL and Ada. Other languages that can call PL/SQL procedures, as well as other database engines, only use late binding.