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 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...