Early Binding and Late binding
The
compiler performs a process called binding when an object is assigned to an
object variable. The early binding (static binding) refers to compile time
binding and late binding (dynamic binding) refers to runtime binding.
Early
Binding (Static binding)
When perform Early Binding, an object is
assigned to a variable declared to be of a specific object type. Early binding
objects are basically a strong type objects or static type objects. While Early
Binding, methods, functions and properties which are detected and checked
during compile time and perform other optimizations before an application
executes. The biggest advantage of using early binding is for performance and
ease of development.Ex:
System.IO.FileStream FS ;
FS = new System.IO.FileStream("C:\\temp.txt", System.IO.FileMode.Open);
Above
code, create a variable FS to hold a new object and then assign a new object to
the variable. Here type is known before the variable is exercised during
run-time, usually through declarative means. The FileStream is a specific
object type, the instance assigned to FS is early bound. Early Binding is also
called static binding or compile time binding.While prforming Early Binding the compiler can ensure at compile time that the function will exist and be callable at runtime. Moreover the compiler guarantees that the function takes the exact number of arguments and that they are of the right type and can checks that the return value is of the correct type.
Late
binding (Dynamic binding)
By
contrast, in Late binding functions, methods, variables and properties are
detected and checked only at the run-time. It implies that the compiler does
not know what kind of object or actual type of an object or which methods or
properties an object contains until run time. The biggest advantages of Late
binding is that the Objects of this type can hold references to any object, but
lack many of the advantages of early-bound objectsEx:
object FS = null;
FS = CreateObject("Scripting.FileSystemObject");
Above
code does not require a reference to be set beforehand, the instance creation
and type determination will just happen at runtime. It is important to note
that the Late binding can only be used to access type members that are declared
as Public. Accessing members declared as Friend or Protected Friend resulted in
a run-time error.While perform late binding there is a possibility of the target function may not exist. Also the target function may not accept the arguments passed to it, and may have a return value of the wrong type.
Summary:
The
Early Binding just means that the target method is found at compile time while
in Late Binding the target method is looked up at run time. Most script
languages use late binding, and compiled languages use early binding.
Method
Overloading and Method Overriding
Method
Overloading happens at compile time (Early Binding) while Overriding happens at
runtime (Late Binding). In method overloading, method call to its definition
has happens at compile time (Static Binding) while in method overriding, method
call to its definition happens at runtime (Dynamic Binding).
Early
bound
The compiler can work out where the called function will be at
compile time.
The compiler can guarantee early (before any of the programs
code runs) that the function will exist and be callable at runtime.
The compiler guarantees that the function takes the right
number of
arguments and that they are of the correct type. It also
checks that the
return value is of the correct type.
Late-binding
The lookup will take longer because its not a simple offset
calculation, there are usually text comparisons to be made.
The target function may not exist.
The target function may not accept the arguments passed to it,
and may have a return value of the wrong type.
With some implementations, the target method can actually
change at
run-time. So, the lookup may execute a different function. I
think this
happens in the Ruby language, you can define a new method on
an object
while the program is running. Late-binding allows function
calls to
start calling a new override for a method instead of calling
the
existing base method.
No comments:
Post a Comment