Dynamic Binding

     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;
     procedure Give_Info(M : Motor_Vehicle);

     type Private_Car isnew Motor_Vehicle with private;
     procedure Give_Info(P: Private_Car);

     type Van is new Motor_Vehicle with private;
     procedure Give_Info(VN: Van);

     type Bus is new Motor_Vehicle with private;
     procedure Give_Info(B: Vehicle);

     type Minibus is new Bus with private;

private
     type Vehicle is tagged null record;

     type Motor_Vehicle is new Vehicle with
         record
              Reg_Number : string(1..7);
         endrecord;

     type Private_Car is new Motor_Vehicle with
         record
              Number_Of_Seats : Positive;
         endrecord;

     type Van is new Motor_Vehicle with
         record
              Max_Load : Positive;
         end record;

     type Bus is new Motor_Vehicle with
         record
              Number_Of_Passengers : Positive;
              Air_Conditioning : Boolean;
         end record;

     type Minibus is new Bus with
         null record;
end Vehicle_Package;
____________________________________________________

package body Vehicle_Package is
     procedure Give_Info (V : Vehicle) is
     begin
         Put_Line("A vehicle");
     end Give_Info;
 

     procedure Give_Info (M : Motor_Vehicle) is
     begin
         Put_Line("A motor-vehicle");
         Put_Line("Reg no: " & M.Reg_Number);
     end Give_Info;
 

     procedure Give_Info (P : Private_Car) is
     begin
         Give_Info(Motor_Vehicle(P));
         Put_Line("A private car");
         Put(P.Number_Of_Seats, Width => 1);
         Put_Line (" seats");
     end Give_Info;
 

     procedure Give_Info (Motor_Vehicle(VN: Van)) is
     begin
         Give_Info(Motor_Vehicle(VN));
         Put_Line("A van");
         Put(VN.Max_Load, Width => 1);
         Put_Line (" kg max load");
     end Give_Info;
 

     procedure Give_Info (B: Bus) is
     begin
         Give_Info(Motor_Vehicle(B));
         Put_Line("A bus");
         Put(B.Number_Of_Passengers, Width => 1);
         Put_Line (" passengers");
         if B.Air_Conditioning then
             Put_Line ("With air conditioning");
         end if;
     end Give_Info;
end Vehicle_Package;

Figure 1  Ada 95 Dynamic binding and polymorphism example

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