There is a virtual method in the baseclass that is overridden in the subclass.
However, I need to add a new parameter in the subclass method and it's impossible to use the "override" declaration, since the parameters are different.
Example:
type
TFruit = class(TObject)
public
constructor Create; virtual;
end;
TApple = class(TFruit)
public
constructor Create(Color: TColor); override; // Error: Declaration of 'Create' differs from previous declaration
end;
I know a good practice in this situation would be creating a new method with another name, but a lot of code would be redundant. This new parameter will affect just a few lines...
Then I thought of using "overload", but then I cannot use "override" together. So I ask: is there any problem in only using "overload"? (Delphi shows a Warning: Method 'Create' hides virtual method of base type 'TFruit')
I also checked the use of reintroduce + overload (to hide the warning above), but I also saw bad recommendations about this practice. What do you think?
And last, what if I just don't use none of them, just removing "override" in the subclass method and add the new param? (which gives me the same warning)
Anyone have any suggestion about what I should do in this case, to keep the good practices?
type
TFruit = class(TObject)
public
constructor Create; virtual;
end;
TApple = class(TFruit)
public
constructor Create; override;
// What should I do:
// constructor Create(Color: TColor); overload; // Shows a warning
// constructor Create(Color: TColor); reintroduce; overload; // Hides the warning
// constructor Create(Color: TColor); // Shows a warning
// Other solution?
end;
Thanks!