Delphi Error – E2062 Virtual constructors are not allowed

Delphi Compiler Error

E2062 Virtual constructors are not allowed

Reason for the Error & Solution

Unlike class types, object types can only have static constructors.

program Produce;

type
  TMyObject = object
    constructor Init; virtual;
  end;

constructor TMyObject.Init;
begin
end;

begin
end.

The example tries to declare a virtual constructor, which does not really make sense for object types and is therefore illegal.

program Solve;

type
  TMyObject = object
    constructor Init;
  end;

constructor TMyObject.Init;
begin
end;

begin
end.

The solution is to either make the constructor static, or to use a new-style class type which can have a virtual constructor.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...