Inherited: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
Title: Inherited inherited
| Old | New | | Line |
|---|
| 1 | | - | Back to [[Reserved words]]. |
| 2 | | - | |
| 3 | | - | In an overridden virtual [[Method|method]], it is often necessary to call the parent [[Class|class]]’ implementation of the virtual method. This can be done with the `inherited` [[Reserved word|reserved word]]. Likewise, the `inherited` [[Keyword|keyword]] can be used to call any method of the parent `class`. |
| 4 | | - | |
| 5 | | - | This case is the simplest: |
| 6 | | - | |
| 7 | | - | ``` pascal |
| 8 | | - | Type |
| 9 | | - | TMyClass = Class(TComponent) |
| 10 | | - | Constructor Create(AOwner : TComponent); override; |
| 11 | | - | end; |
| 12 | | - | |
| 13 | | - | Constructor TMyClass.Create(AOwner : TComponent); |
| 14 | | - | begin |
| 15 | | - | Inherited; |
| 16 | | - | // Do more things |
| 17 | | - | end; |
| 18 | | - | ``` |
| 19 | | - | |
| 20 | | - | ## Constructors and destructors case |
| 21 | | - | |
| 22 | | - | [[Constructor]], example 1 : |
| 23 | | - | |
| 24 | | - | ``` pascal |
| 25 | | - | ... |
| 26 | | - | TTest.Create; |
| 27 | | - | begin |
| 28 | | - | Inherited; // Always at the beginning of the constructors and start the constructor (code only) of the parent class |
| 29 | | - | ... |
| 30 | | - | end; |
| 31 | | - | ``` |
| 32 | | - | |
| 33 | | - | `Constructor`, example 2 : |
| 34 | | - | |
| 35 | | - | ``` pascal |
| 36 | | - | ... |
| 37 | | - | TTest.Create(...); |
| 38 | | - | begin |
| 39 | | - | Inherited Create(...); // Always at the beginning of the constructors and start the constructor (code only) of the parent class |
| 40 | | - | ... |
| 41 | | - | end; |
| 42 | | - | ... |
| 43 | | - | ``` |
| 44 | | - | |
| 45 | | - | [[Destructor]], example 3 : |
| 46 | | - | |
| 47 | | - | ``` pascal |
| 48 | | - | TTest.Destroy; |
| 49 | | - | begin |
| 50 | | - | ... |
| 51 | | - | Inherited; // Always at the end of the destructors and start the destructor (code only) of the parent class |
| 52 | | - | end; |
| 53 | | - | ... |
| 54 | | - | ``` |
| 55 | | - | |
| 56 | | - | ## Virtual methods override |
| 57 | | - | |
| 58 | | - | ``` pascal |
| 59 | | - | type |
| 60 | | - | TMyClass = class(TStrings) |
| 61 | | - | function GetObject(Index: Integer): TObject; override; |
| 62 | | - | end; |
| 63 | | - | |
| 64 | | - | function TMyClass.GetObject(Index: Integer): TObject; |
| 65 | | - | begin |
| 66 | | - | // Get result from parent class method |
| 67 | | - | Result := inherited GetObject(Index); |
| 68 | | - | // Do something |
| 69 | | - | end; |
| 70 | | - | ``` |
| 71 | | - | |
| 72 | | - | --- |
| 73 | | - | *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](/wiki-contact).* |
| 1 | + | #REDIRECT [[Inherited]] |
Back to history