HomeDelphiDelphi Error – X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s)

Delphi Error – X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s)

Delphi Compiler Error

X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s)

Reason for the Error & Solution

The method named in the error message has been declared as an override of a virtual method in a base class, but the visibility in the current class is lower than that used in the base class for the same method.

While the visibility rules of Delphil would seem to indicate that the function cannot be seen, the rules of invoking virtual functions will cause the function to be properly invoked through a virtual call.

Generally, this means that the method of the derived class was declared in a private or protected section while the method of the base class was declared in a protected or pubic (including published) section, respectively.

unit Produce;
interface

  type
    Base = class(TObject)
    public
      procedure VirtualProcedure(X: Integer); virtual;
    end;

    Extended = class(Base)
    protected
      procedure VirtualProcedure(X: Integer); override;
    end;

implementation

  procedure Base.VirtualProcedure(X: Integer);
  begin
  end;

  procedure Extended.VirtualProcedure(X: Integer);
  begin
  end;
end.


The example above aptly shows how this error is produced by putting Extended.VirtualProcedure into the protected section.

In practice this is never harmful, but it can be confusing to someone reading documentation and observing the visibility attributes of the document.

This hint will only be produced for classes appearing in the interface section of a unit.

unit Solve;
interface

  type
    Base = class(TObject)
    public
      procedure VirtualProcedure(X: Integer); virtual;
    end;

    Extended = class(Base)
    public
      procedure VirtualProcedure(X: Integer); override;
    end;

implementation

  procedure Base.VirtualProcedure(X: Integer);
  begin
  end;

  procedure Extended.VirtualProcedure(X: Integer);
  begin
  end;
end.


There are three basic solutions to this problem.

  1. Ignore the hint
  2. Change the visibility to match the base class
  3. Move class definition to the implementation section.

The example program above has taken the approach of changing the visibility to match the base class.

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 X2243 Expression needs no Initialize/Finalize Reason for the Error & Solution You have attempted to use the...