6.6.9 Covariant return type

When overriding a function that has a class typed result, it is possible to change the exact class to a descendant of the result type of the parent method.

This means that the following will compile:

Program Project1;
{$mode objfpc}
type
  TClass1 = class
  public
    function A: TClass1; virtual; abstract;
  end;

  TClass2 = class(TClass1)
  public
    function A: TClass2; override; // result differs: TClass2 as descendant of TClass1
  end;

{ TClass2 }

function TClass2.A: TClass2;
begin
  Result := Self;
end;

begin
end.

The covariant return type can be used in ObjFPC and Delphi modes.