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.