Section 7.3 - Dynamic Dispatching (Polymorphism) in Ada

Ada 95 also includes the concept of a class. For each tagged type T there is an associated type T'Class; this type comprises the union of all types in the tree of derived types rooted at T. For example, in the previous example, Rectangle'Class is a class that includes the types Rectangle and Square; Figure'Class is a class that includes the types Circle, Rectangle, and Square.

Note that Ada 95 has a more specific meaning for ``class'' than some other object-oriented programming languages (such as C++). In C++, the term class may mean either ``a specific type'' or ``the set of a specific types and all types that inherit from it, directly and indirectly.'' Ada 95 uses different terms for these different concepts.

A subprogram can define one or more parameters as a type of the form T'Class. Any type T, or any of its descendents, is acceptable for a parameter of type T'Class. You can pass that parameter on to any other subprogram that expects the same T'Class without a problem. But what happens if that subprogram that accepted something of T'Class then tries to call some other subprogram that requires a specific type? The answer is dynamic dispatching.

Dynamic dispatching is simply the process of determining at run-time exactly which routine to call, and then calling it. How can Ada figure that out? The answer is that every value that is a tagged type includes an invisible "tag" that tells Ada the actual type. This tag is used to figure out which routine to call. Because of the compilation rules Ada imposes, Ada can guarantee that dynamic dispatching will always find a subprogram to call.

Again, this is easier shown than explained. Let's say that we want to create a subprogram that takes in any kind of Figure. The subprogram is to print out the phrase "Area =" and then call the correct Area subprogram for any type of figure. Here's an example of how to do that:

 procedure Print_Area(F : in Figure'Class) is
   Put("Area = ");
   Put( Area(F) );  -- Call the correct area-computing routine for F
                    -- and then print its result.
 end Print_Area;

When the Ada program reaches the Area(F) statement, it will note that the current type is a Class type and that Area takes only a specific type. Therefore Ada dispatch to the correct Area operation depending on the specific type of the input value F.

Only subprograms defined in the same package as the tagged type can be dispatched. These subprograms are formally called primitive subprograms. Primitive subprograms should generally be defined immediately after the type is defined.


In the following package:

 package Critters is
   type Monster is tagged
        Size_In_Meters : Integer;
      end record;
   type Dragon is new Monster with null record;
   type Red_Dragon is new Dragon with null record;
   type Person is tagged
        Age : Integer;
      end record;
 end Critters;

How many different types does Monster'Class include in package Critters?

  1. 1
  2. 2
  3. 3
  4. 4

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 7 outline

David A. Wheeler (

The master copy of this file is at "".