Inherited
Deutsch · English · Français · Suomi · Русский
Back to Reserved words.
In an overridden virtual method, it is often necessary to call the parent class’ implementation of the virtual method. This can be done with the inherited reserved word. Likewise, the inherited keyword can be used to call any method of the parent class.
This case is the simplest:
Type
TMyClass = Class(TComponent)
Constructor Create(AOwner : TComponent); override;
end;
Constructor TMyClass.Create(AOwner : TComponent);
begin
Inherited;
// Do more things
end;Constructors and destructors case
Constructor, example 1 :
...
TTest.Create;
begin
Inherited; // Always at the beginning of the constructors and start the constructor (code only) of the parent class
...
end;Constructor, example 2 :
...
TTest.Create(...);
begin
Inherited Create(...); // Always at the beginning of the constructors and start the constructor (code only) of the parent class
...
end;
...Destructor, example 3 :
TTest.Destroy;
begin
...
Inherited; // Always at the end of the destructors and start the destructor (code only) of the parent class
end;
...Virtual methods override
type
TMyClass = class(TStrings)
function GetObject(Index: Integer): TObject; override;
end;
function TMyClass.GetObject(Index: Integer): TObject;
begin
// Get result from parent class method
Result := inherited GetObject(Index);
// Do something
end;From the Free Pascal wiki, page "Inherited", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
