You simply enter the constructor with the name of the ancestor. Once you do this, the user will not be able to create a TMyObject call to the constructor introduced in TObject . If you use code like this:
TMyObject = class public constructor Create; end; constructor TMyObject.Create; begin // I am not calling the inherited constructor because // I do not want to. end;
You do not use the override modifier on TMyObject.Create because the parent constructor is not virtual.
Using this scheme, the user cannot create your TMyObject using the constructor introduced in the ancestor. In this case, the ancestor is TObject , and its only constructor is TObject.Create . If the user writes this code:
X := TMyObject.Create;
quite obviously, the TMyObject constructor will be called, not the one introduced in TObject .
If you are afraid that users will jump through hoops to create their own class using the ancestor constructor, you can make your own material from the AfterConstruction method. This is a virtual method, so it is called even if your object is created using an ancestor type class reference:
TMyObject = class public procedure AfterConstruction;override; end;
Cosmin prund
source share