When the words dynamic binding are used, it actually means dynamic binding of an operation to an object. In computer programming there are two types of binding, static and dynamic.
Static binding is when it is known which object will be linked to a particular function at compile time. All parameters for static binding are of a specific type.
Dynamic binding is when you can not determine which object will be linked to a particular function until run time. One or more of the parameters are of a polymorphic type.
In some books you will find static binding called early binding and dynamic binding called late binding.
Figure 1 is a program example taken
from the book Ada 95 From the Beginning showing an example of dynamic
binding and polymorphism. This same example is shown in the polymorphism
section.
| package Vehicle_Package is
type Vehicle is tagged private; procedure Give_Info(V: Vehicle); type Motor_Vehicle isnew
Vehicle with private;
type Private_Car isnew
Motor_Vehicle with private;
type Van is
new
Motor_Vehicle with private;
type Bus is
new
Motor_Vehicle with private;
type Minibus is new Bus with private; private
type Motor_Vehicle is
new Vehicle with
type Private_Car is
new Motor_Vehicle with
type Van is new
Motor_Vehicle with
type Bus is new
Motor_Vehicle with
type Minibus is new
Bus with
package body Vehicle_Package is
procedure Give_Info (M
: Motor_Vehicle) is
procedure Give_Info (P
: Private_Car) is
procedure Give_Info (Motor_Vehicle(VN:
Van)) is
procedure Give_Info (B:
Bus) is
|
Using figure 1, if we declare the following variables:
PC : aliased Private_car;
VV : aliased Van;
MB : aliased Minibus;
If we make the call:
Give_Info (PC); -- static binding
This would be static binding and give an output that would look something like this:
A motor-vehicle
Reg no: L970KAT
A private car
4 seats
Dynamic binding would have a declaration that looks like this:
type Vehicle_Pointer is access
all Vehicle'Class;
VP : Vehicle_Pointer;
If we make the call:
VP := VV'Access;
Give_Info(VP.all); --
dynamic binding
This would be dynamic binding and give an output that would look something like this:
A motor-vehicle
Reg no: T123ZZY
A van
2500 kg max load
If type Vehicle has root T, then the type Vehicle's polymorphic type is designated T'Class. The word 'Class' means 'any of the specific types which comprise T's type family'. T'Class is known as a class-wide type.
Family of types, for a given type T is defined as the
type T itself and all the types which have T as parent, grandparent, great-grandparent,
ect.
Go to Assertions or the Table of Contents