ZetCode

Ada inheritance and polymorphism

last modified November 23, 2026

A tagged type can be extended, and an extension inherits the operations of its parent. The interesting question is what an operation does when the object belongs to an extension: the same call can reach the inherited body or a new body written for it. This article overrides operations, dispatches through class-wide types, declares abstract types and interfaces, and finishes with a collection of figures kept behind one access type.

An extension without new components

The first program repeats the tagged record from the previous article and adds Square, an extension that declares no components of its own. The syntax with null record says that there is nothing to add. A Square is nevertheless a distinct type, and it inherits every operation of Shape.

shapes.ads
package Shapes is
   type Shape is tagged record
      Width  : Natural := 1;
      Height : Natural := 1;
   end record;

   function Area (S : Shape) return Natural;

   type Square is new Shape with null record;
end Shapes;

Square is new Shape with null record. The extension inherits Width, Height, and Area; no operation is declared for it.

shapes.adb
package body Shapes is

   function Area (S : Shape) return Natural is
   begin
      return S.Width * S.Height;
   end Area;

end Shapes;

The body of Area multiplies the two components and knows nothing about Square.

shapes_main.adb
with Ada.Text_IO;
with Shapes;
use Ada.Text_IO;

procedure Shapes_Main is
   Box  : constant Shapes.Shape := (Width => 3, Height => 4);
   Tile : constant Shapes.Square := (Width => 5, Height => 1);
begin
   Put_Line ("box area  =" & Natural'Image (Box.Area));
   Put_Line ("tile area =" & Natural'Image (Tile.Area));
end Shapes_Main;

A Tile is an aggregate with the inherited components. Tile.Area passes the square to the inherited function, which returns 5 * 1 = 5, while Box.Area returns 12.

$ gnatmake -gnat2022 -gnatwa shapes_main.adb
$ ./shapes_main
box area  = 12
tile area = 5

Overriding an inherited operation

An extension can replace an inherited operation with a body of its own. The profile has to stay the same, and the keyword overriding states the intention: the compiler rejects the declaration if no inherited operation matches it. A square stores a height, but its area depends only on the width, so its Area ignores Height.

shapes.ads
package Shapes is
   type Shape is tagged record
      Width  : Natural := 1;
      Height : Natural := 1;
   end record;

   function Area (S : Shape) return Natural;

   type Square is new Shape with null record;

   overriding function Area (S : Square) return Natural;
end Shapes;

The new declaration of Area appears after the extension in the same package specification, so it is a primitive operation of Square.

shapes.adb
package body Shapes is

   function Area (S : Shape) return Natural is
   begin
      return S.Width * S.Height;
   end Area;

   function Area (S : Square) return Natural is
   begin
      return S.Width * S.Width;
   end Area;

end Shapes;

Both bodies have the same name; the type of the parameter chooses which one runs. Inside Area (S : Square), the expression S.Width reads the inherited component.

shapes_main.adb
with Ada.Text_IO;
with Shapes;
use Ada.Text_IO;

procedure Shapes_Main is
   Box  : constant Shapes.Shape := (Width => 3, Height => 4);
   Tile : constant Shapes.Square := (Width => 5, Height => 1);
   View : constant Shapes.Shape := Shapes.Shape (Tile);
begin
   Put_Line ("box area   =" & Natural'Image (Box.Area));
   Put_Line ("tile area  =" & Natural'Image (Tile.Area));
   Put_Line ("parent area =" & Natural'Image (View.Area));
end Shapes_Main;

Tile.Area now returns 25. The conversion Shapes.Shape (Tile) gives a view of the parent part of the object, and assigning that view to View copies only the parent components. The call View.Area has a Shape argument, so it uses the parent body and returns 5 * 1 = 5. The conversion also changes the tag: View is a Shape, not a Square.

$ gnatmake -gnat2022 -gnatwa shapes_main.adb
$ ./shapes_main
box area   = 12
tile area  = 25
parent area = 5

Class-wide types and dispatching

A class-wide type, written T'Class, contains T and all of its extensions. A parameter of type Shape'Class accepts a Shape, a Square, or a Circle. A call of a primitive operation with a class-wide controlling operand is a dispatching call: the body is chosen at run time from the tag of the actual object.

shapes.ads
package Shapes is
   type Shape is tagged record
      Width  : Natural := 1;
      Height : Natural := 1;
   end record;

   function Area (S : Shape) return Natural;

   type Square is new Shape with null record;

   overriding function Area (S : Square) return Natural;

   type Circle is new Shape with record
      Radius : Natural := 1;
   end record;
   overriding function Area (C : Circle) return Natural;
end Shapes;

Circle extends Shape with a radius. It inherits Width and Height, which its Area does not use, but an aggregate still has to supply them.

shapes.adb
package body Shapes is

   function Area (S : Shape) return Natural is
   begin
      return S.Width * S.Height;
   end Area;

   function Area (S : Square) return Natural is
   begin
      return S.Width * S.Width;
   end Area;

   function Area (C : Circle) return Natural is
   begin
      return 3 * C.Radius * C.Radius;
   end Area;

end Shapes;

Three bodies of Area exist now. The compiler builds a dispatch table for the class-wide type, and a dispatching call reaches the right body through it.

shapes_main.adb
with Ada.Text_IO;
with Shapes;
use Ada.Text_IO;

procedure Shapes_Main is
   Box  : constant Shapes.Shape := (Width => 3, Height => 4);
   Tile : constant Shapes.Square := (Width => 5, Height => 1);
   Disk : constant Shapes.Circle := (Width => 1, Height => 1, Radius => 2);

   procedure Describe (S : Shapes.Shape'Class) is
   begin
      Put_Line ("area =" & Natural'Image (S.Area));
   end Describe;
begin
   Describe (Box);
   Describe (Tile);
   Describe (Disk);
end Shapes_Main;

Describe takes Shape'Class, and the call S.Area inside it dispatches. The three calls print 12, 25, and 12. The result depends on the tag of the argument, not on the text of Describe, which never mentions Square or Circle.

$ gnatmake -gnat2022 -gnatwa shapes_main.adb
$ ./shapes_main
area = 12
area = 25
area = 12

Abstract types

Often the parent cannot supply a sensible body at all: there is no general formula for the area of a figure. The declaration abstract tagged introduces a type that cannot have objects, and an abstract function declares an operation with no body. A concrete extension has to override every abstract operation it inherits, otherwise it is abstract too.

figures.ads
package Figures is
   type Figure is abstract tagged record
      Name : String (1 .. 8);
   end record;

   function Area (F : Figure) return Natural is abstract;

   procedure Show (F : Figure'Class);

   type Rect is new Figure with record
      Width  : Natural := 1;
      Height : Natural := 1;
   end record;
   overriding function Area (R : Rect) return Natural;

   type Disk is new Figure with record
      Radius : Natural := 1;
   end record;
   overriding function Area (D : Disk) return Natural;
end Figures;

Figure is abstract and Area is abstract. Rect and Disk are concrete extensions, and both override Area. Show takes Figure'Class, so it can display any figure, and F.Area inside it dispatches.

figures.adb
with Ada.Text_IO;

package body Figures is
   use Ada.Text_IO;

   procedure Show (F : Figure'Class) is
   begin
      Put (F.Name);
      Put_Line (Natural'Image (F.Area));
   end Show;

   function Area (R : Rect) return Natural is
   begin
      return R.Width * R.Height;
   end Area;

   function Area (D : Disk) return Natural is
   begin
      return 3 * D.Radius * D.Radius;
   end Area;

end Figures;

Show prints the name and then the area. The area comes from the body of the actual type; the two concrete Area bodies are the only implementations in the program.

figures_main.adb
with Ada.Text_IO;
with Figures;
use Ada.Text_IO;

procedure Figures_Main is
   Wall  : constant Figures.Rect :=
     (Name => "wall    ", Width => 4, Height => 3);
   Wheel : constant Figures.Disk :=
     (Name => "wheel   ", Radius => 2);
begin
   Wall.Show;
   Wheel.Show;
   Put_Line ("total =" & Natural'Image (Wall.Area + Wheel.Area));
end Figures_Main;

Wall is a Rect and Wheel is a Disk. Both calls to Show dispatch, while Wall.Area in the last line is a normal static call, because Wall has a specific type.

$ gnatmake -gnat2022 -gnatwa figures_main.adb
$ ./figures_main
wall     12
wheel    12
total = 24

No objects of an abstract type

An abstract type describes a class of types; it does not describe a value that can exist on its own. Declaring a variable of the abstract type is therefore rejected, even though the type is visible and its operations can be called through a class-wide view.

abstract_main.adb
with Figures;

procedure Abstract_Main is
   F : Figures.Figure;
begin
   null;
end Abstract_Main;

The compiler refuses to declare an object of an abstract type. A variable needs a concrete type; class-wide values are passed as parameters or reached through access values instead.

$ gnatmake -gnat2022 -gnatwa abstract_main.adb
x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa abstract_main.adb
abstract_main.adb:4:15: error: type of object cannot be abstract
gnatmake: "abstract_main.adb" compilation error

Interfaces

Ada has single inheritance for tagged types, but a type may implement several interfaces. An interface is an abstract type with no components; it declares operations that every implementing type has to provide. Here Named requires a Label function, and Disk is declared as new Figure and Named.

figures.ads
package Figures is
   type Named is interface;
   function Label (N : Named) return String is abstract;

   type Figure is abstract tagged record
      Name : String (1 .. 8);
   end record;

   function Area (F : Figure) return Natural is abstract;

   procedure Show (F : Figure'Class);

   type Rect is new Figure with record
      Width  : Natural := 1;
      Height : Natural := 1;
   end record;
   overriding function Area (R : Rect) return Natural;

   type Disk is new Figure and Named with record
      Radius : Natural := 1;
   end record;
   overriding function Area (D : Disk) return Natural;
   overriding function Label (D : Disk) return String;
end Figures;

The interface adds Label to the operations that Disk must implement. Rect does not implement Named, so it has no Label.

figures.adb
with Ada.Text_IO;

package body Figures is
   use Ada.Text_IO;

   procedure Show (F : Figure'Class) is
   begin
      Put (F.Name);
      Put_Line (Natural'Image (F.Area));
   end Show;

   function Area (R : Rect) return Natural is
   begin
      return R.Width * R.Height;
   end Area;

   function Area (D : Disk) return Natural is
   begin
      return 3 * D.Radius * D.Radius;
   end Area;

   function Label (D : Disk) return String is
   begin
      return "disk of radius" & Natural'Image (D.Radius);
   end Label;

end Figures;

Label builds a string from the radius. The expression Natural'Image supplies the leading space before the number, so the text reads disk of radius 2.

figures_main.adb
with Ada.Text_IO;
with Figures;
use Ada.Text_IO;

procedure Figures_Main is
   Wall  : constant Figures.Rect :=
     (Name => "wall    ", Width => 4, Height => 3);
   Wheel : constant Figures.Disk :=
     (Name => "wheel   ", Radius => 2);
begin
   Wall.Show;
   Wheel.Show;
   Put_Line (Wheel.Label);
   Put_Line ("total =" & Natural'Image (Wall.Area + Wheel.Area));
end Figures_Main;

Wall.Show and Wheel.Show dispatch as before, and Wheel.Label calls the interface operation on the Disk. The interface type itself has no objects; it only describes what an implementing type must offer.

$ gnatmake -gnat2022 -gnatwa figures_main.adb
$ ./figures_main
wall     12
wheel    12
disk of radius 2
total = 24

An applied example: a gallery

The final program keeps a collection of figures behind access values of type Figure'Class. Because the designated type is class-wide, one access type can point to a Rect or to a Disk, and a call through the pointer dispatches. The array is an Ada 2022 aggregate written with square brackets.

gallery_main.adb
with Ada.Text_IO;
with Figures;
use Ada.Text_IO;

procedure Gallery_Main is
   type Figure_Access is access all Figures.Figure'Class;

   Pictures : constant array (1 .. 3) of Figure_Access :=
     [new Figures.Rect'(Name => "wall    ", Width => 4, Height => 3),
      new Figures.Rect'(Name => "door    ", Width => 2, Height => 5),
      new Figures.Disk'(Name => "wheel   ", Radius => 2)];

   Total : Natural := 0;
begin
   for P of Pictures loop
      P.Show;
      Total := Total + P.Area;
   end loop;
   Put_Line ("total =" & Natural'Image (Total));
end Gallery_Main;

P.Show and P.Area implicitly dereference the access value. The total is computed by dispatching Area for every figure: 12 + 10 + 12 = 34. A new figure type would not change the loop; only the array aggregate would gain one more entry.

$ gnatmake -gnat2022 -gnatwa gallery_main.adb
$ ./gallery_main
wall     12
door     10
wheel    12
total = 34

Next steps

The next article turns to controlled types, whose Initialize, Adjust, and Finalize procedures run automatically when objects are created, copied, and destroyed. They use the tagged types of Ada.Finalization, so the extension syntax shown here is the starting point.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have extended tagged types, overridden a primitive operation, called operations through 'Class and seen them dispatch, declared abstract types and interfaces, and stored class-wide values behind access types. The tagged types article explains the tags and primitives that this article builds on.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.