<p>Does Delphi call inherited on overridden procedures if there is no explicit call in the code ie (inherited;), I have the following structure (from super to sub class)</p>

<p>TForm >> TBaseForm >> TAnyOtherForm</p>

<p>All the forms in the project will be derived from TBaseForm, as this will have all the standard set-up and destructive parts that are used for every form (security, validation ect). </p>

<p>TBaseForm has onCreate and onDestroy procedures with the code to do this, but if someone (ie me) forgot to add inherited to the onCreate on TAnyOtherForm would Delphi call it for me? I have found references on the web that say it is not required, but nowhere says if it gets called if it is omitted from the code.</p>

<p>Also if it does call inherited for me, when will it call it?</p>

[{'answer_id': 53721, 'author': 'Ralph M. Rickenbach', 'author_id': 4549416, 'author_profile': 'https://Stackoverflow.com/users/4549416', 'pm_score': 5, 'selected': True, 'text': '<p>No, if you leave the call to inherited away, it will not be called. Otherwise it would not be possible to override a method and totally ommit the parent version of it.</p>\n'}, {'answer_id': 53785, 'author': 'Frank', 'author_id': 4474, 'author_profile': 'https://Stackoverflow.com/users/4474', 'pm_score': 2, 'selected': False, 'text': '<p>The inherited call has to be made explicitly. In general no language automatically calls the inherited function in equivalent situations (class constructors not included). </p>\n\n<p>It is easy to forget to make the inherited call in a class constructor. In such a situation if a base class needs to initialize any data you have an access violation waiting to happen.</p>\n\n<p>Perhaps you could override DoCreate and DoDestory in your TBaseForm class so you could ensure some code is executed regardless of the implementation of child classes. </p>\n\n<pre><code>// interface\n\nTBaseForm = Class(TForm)\n...\nProtected\n    Procedure DoCreate(Sender : TObject); Override;\nEnd\n\n// implementation\n\nProcedure TBaseForm.DoCreate(Sender : TObject);\nBegin\n    // do work here\n\n    // let parent call the OnCreate property  \n    Inherited DoCreate(Sender);\nEnd;\n</code></pre>\n'}, {'answer_id': 53817, 'author': 'mj2008', 'author_id': 5544, 'author_profile': 'https://Stackoverflow.com/users/5544', 'pm_score': 2, 'selected': False, 'text': '<p>It is worth mentioning that not calling inherited in Destroy of any object can cause memory leaks. There are tools available to check for this in your source code. </p>\n'}, {'answer_id': 57513, 'author': 'Jody Dawkins', 'author_id': 1234, 'author_profile': 'https://Stackoverflow.com/users/1234', 'pm_score': 1, 'selected': False, 'text': '<p>The inherited code is not called implicitly, as the others have indicated. You must call it explicitly. This gives you some useful flexibility. For instance, you might want to do some preprocessing code prior to the inherited code, then do some post processing code afterwards. This might look like:</p>\n\n<pre><code>procedure TMyCalcObject.SolveForX;\nbegin\n  ResetCalcState;\n  inherited SolveForX;\n  PostProcessSolveForX;\nend;\n</code></pre>\n'}, {'answer_id': 57519, 'author': 'JosephStyons', 'author_id': 672, 'author_profile': 'https://Stackoverflow.com/users/672', 'pm_score': 0, 'selected': False, 'text': "<p>No.  That's the whole point of overriding.</p>\n"}, {'answer_id': 58172, 'author': 'Jim McKeeth', 'author_id': 255, 'author_profile': 'https://Stackoverflow.com/users/255', 'pm_score': 2, 'selected': False, 'text': '<p>Inherited must be explicitly called in descendant objects as well as in visual form inheritance.  If you use class completion then it adds inherited automatically if you flagged the definition as override (but not for reintroduce).  If you are using visual form inheritance then when you add a new event hander through the form editor then it will add inherited as well.</p>\n'}, {'answer_id': 19009111, 'author': 'Old school', 'author_id': 2815987, 'author_profile': 'https://Stackoverflow.com/users/2815987', 'pm_score': 1, 'selected': False, 'text': '<p>You must call it explicitly. This allows a lot of flexibility, since you can choose at which point in code to call the inherited method. But it\'s also a big source of bugs. It\'s easy to forget to call the inherited function and compiler has no way to tell if you did it deliberately or you just forgot.</p>\n\n<p>There should be some kind of directive "skip_inherited" to tell compiler that you don\'t want to call the inherited method.</p>\n\n<p>Compiler would then easily report error if it didn\'t find either "inherited" or "skip_inherited". That would mean you forgot. But unfortunately nobody in CodeGear thought of that.</p>\n'}]
['https://Stackoverflow.com/questions/53715', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2098/']