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.
- Ignore the hint
- Change the visibility to match the base class
- Move class definition to the implementation section.
The example program above has taken the approach of changing the visibility to match the base class.