Menu:

Design and Sell Merchandise Online for Free

Interface types

Carrying on from the abstract types tutorial, the natural progression is to add multiple inheritance. In Ada95 this cannot be done as the language doesn't supply any features for this - it is possible, however, but requires the use of generics.

In C++ this is easy enough as we can just add another interface (or class) to the list of classes to derive from:

C++ code
class Dragster : public Vehicle, public Parachute {
    bool GearUp();
    bool GearDown();
    void Accelerate();
    void Brake();
    void Deploy(); -- This has been added!
};

For this tutorial we must use the Ada 2005 language as this provides interface types similar to those provided in Java. The interface type is like the abstract type seen in the previous tutorial except that the use of multiple inheritance is allowed. So, in Ada 2005 we would define the Parachute type as:

Ada 2005 code
package Parachute is

  type Parachute_Type is interface;
  
  procedure Deploy(Self : in Parachute_Type) is abstract;
  
end Parachute;

Notice the use of the interface keyword, this is new as of Ada 2005. This package provides the specification of the Deploy operation and again, this is abstract. Interfaces can only have abstract primitives or null primitives (i.e. primitives that do not have to be implemented). We can then create a new Dragster type:

Ada 2005 code
with Vehicle;
with Parachute;

package Dragster is

  type Dragster_Type is new Vehicle.Vehicle_Type and
    Parachute.Parachute_Type with private;

  function Gear_Up(Self : Dragster_Type) return Boolean;
  function Gear_Down(Self : Dragster_Type) return Boolean;
  procedure Accelerate(Self : Dragster_Type);
  procedure Brake(Self : Dragster_Type);
  procedure Deploy(Self : in Dragster_Type);

private

  type Dragster_Type is new Vehicle.Vehicle_Type and
    Parachute.Parachute_Type with null record;
    
end Dragster;

Again, notice the difference in the way we define the type, the and keyword means that we are also deriving this type from the vehicle type and the parachute interface type. The Dragster type has to implement the Deploy primitive, which can then be used:

Ada 2005 code

procedure Test is

  -- Make sure we can accept pointers to all vehicles
  -- in this class.
  type Vehicle_Access is access all
    Vehicle.Vehicle_Type'Class;
  type Parachute_Access is access all
    Parachute.Parachute_Type'Class;

  A      : Vehicle_Access;
  -- Removed some code.
  D      : aliased Dragster.Dragster_Type;
  P      : Parachute_Access;
  Result : Boolean;

begin

  -- Removed some code.

  A      := Vehicle.Vehicle_Type(D)'Access;
  -- Call's Dragster.Gear_Up
  Result := Vehicle.Gear_Up(A.all);
  
  -- Use polymorphism on the Parachute type.
  P      := D'Access;

  Parachute.Deploy(P.all);
  
end Test;

I have placed a full example into a compressed text file which can be built with GNAT using the following commands:

Bash code
$ gunzip interface_types.ada.gz
$ gnatchop -cw -gnat05 interface_types.ada
$ gnatmake -gnat05 test.adb

Other Ada 2005 compilers will differ.

Download interface_types.ada.gz

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
Security Code: *