Search code examples
delphioverloadingvirtual-functions

How do I overload a virtual function introduced in a parent class?


I have a parent class with one important abstract procedure which I am overloading in many child classes as the example code shown below:

TCParent = Class
private
public
 procedure SaveConfig; virtual; abstract;

end;

TCChild = Class(TCParent)
private
public
 procedure SaveConfig; override;
end;

Now there I need to (overload) this procedure with another SaveConfig procedure that will accept parameters, yet I don't want to make big changes in the parent class that might require that I go and make changes in all other child classes.

Is there a way I can overload SaveConfig in this specific child class without making big changes to the parent class and other child classes that inherit from it?


Solution

  • You can use reintroduce to add a new overloaded method. Note that the order of reintroduce; overload; in the child class is required; if you reverse them, the code won't compile.

    TCParent = Class
    private
    public
     procedure SaveConfig; virtual; abstract;
    end;
    
    TCChild = Class(TCParent)
    private
    public
     procedure SaveConfig; overload; override;
     procedure SaveConfig(const FileName: string); reintroduce; overload;
    end;
    

    (Tested in Delphi 7, so should work in it and all later versions.)