Delphi Error – E2170 Cannot override a non-virtual method

Delphi Compiler Error

E2170 Cannot override a non-virtual method

Reason for the Error & Solution

You have tried, in a derived class, to override a base method which was not declared as one of the virtual types.

program Produce;

  type
    Base = class
      procedure StaticMethod;
    end;

    Derived = class (Base)
      procedure StaticMethod; override;
    end;

    procedure Base.StaticMethod;
    begin
    end;

    procedure Derived.StaticMethod;
    begin
    end;

begin
end.

The example above elicits an error because Base.StaticMethod is not declared to be a virtual method, and as such it is not possible to override its declaration.

program Solve;

  type
    Base = class
      procedure StaticMethod;
    end;

    Derived = class (Base)
      procedure StaticMethod;
    end;

    procedure Base.StaticMethod;
    begin
    end;

    procedure Derived.StaticMethod;
    begin
    end;

begin
end.

The only way to remove this error from your program, when you don’t have the source for the base classes, is to remove the ‘override’ specification from the declaration of the derived method. If you have source to the base classes, you could, with careful consideration, change the base’s method to be declared as one of the virtual types. Be aware, however, that this change can have a drastic affect on your programs.

Share:

Leave A Reply

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

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...