Sajun.org
In
computer science, '''binding''' is associating objects and implementations with names in
programming language so that those objects and implementaions can be accessed by the names. An object's names are said to be "bound" to them.
Deep binding and
shallow bindings are not kinds of binding, but ways to implement binding.
The simplest example is defining
subprograms:
def sum (x, y)
x + y
end
sum (2, 3)
In this
Ruby programming language code, an implementation returning the sum of given two inputs is ''bound'' to a name,
sum.
== Binding time ==
Because objects in computer programs are usually resident in the computer memory, binding time is almost the same as the time of allocation of memory space for objects. Bind can be done either at
compile-time or
link-time (static binding) or at
run-time (dynamic binding). Also, scope rules might define binding time of objects. Local variables, for example, are usually bound at
run-time while global variables at
compile-time.
For example,
static int n;
int main ()
{
n = 12;
return n;
}
In this
C code, a global variable ''n'' is bound to certain location in the
memory of the
computer.
=== Dynamic binding for polymorphism ===
In
object-oriented programming, an object can respond to the same message with a different implementation. Dynamic binding is a common solution for this problem.
For example there may be an object that contains the ''name'' (data) 'Socrates', and which is of the class (or
type) Person.
Now suppose all Persons are mortal. In object oriented programming, we can say that the Person class must implement the Mortal interface, which contains the method die().
Persons and Plants die in different ways, for example Plants don't stop breathing. 'Dynamic binding' is the practice of figuring out which method to invoke at runtime. For example, if we write
void kill(Mortal m) {
m.die();
}
it's not clear whether m is a Person or a Plant, and thus whether Plant.die() or Person.die() should be invoked on the object. With dynamic binding, the m object is examined at runtime, and the method corresponding to its actual class is invoked. (This implies that the actual representation of an object in memory is just its data and doesn't include the methods.)