The constructor of a class does not call the constructors of any of its ancestor classes by
default. This means that the fields of any ancestor classes will not be initialized, potentially
causing unexpected program errors. The constructor of the ancestor class can be called using the
inherited keyword.
Note that while TObject does not contain any code in its constructor or destructor,
the inherited keyword should still be used for immediate descendants. This is for
consistency and to guard against bugs that may emerge if the class's parent is changed from
TObject in future.
If there is a parent constructor with the same parameters, adding an inherited
to the beginning of the method will call it:
constructor TParent.Create; // ... end; constructor TChild.Create; begin FMyStr := 'hello!'; end;
constructor TParent.Create; // ... end; constructor TChild.Create; begin inherited; FMyStr := 'hello!'; end;
Otherwise, the desired parent constructor can be called using an inherited keyword
followed by its name and arguments:
constructor TParent.Create(MyInt: Integer); // ... end; constructor TChild.Create(MyStr: string; MyInt: Integer); begin FMyStr := MyStr; end;
constructor TParent.Create(MyInt: Integer); // ... end; constructor TChild.Create(MyStr: string; MyInt: Integer); begin inherited Create(MyInt); FMyStr := MyStr; end;