delphiinheritance

What's the reason for the TCustomXXX classes in Delphi class hierarchy?


This is probably somewhat dumb or even explained in the docs but I couldn't find it.

There's a pattern that seems to repeat among a lot of Delphi classes which is the creation of a TCustomSomething before the actual TSomething. I wonder if there's any special reason for that and in case I want to create a derivation of TSomething should I inherit from TCustomSomething or directly from TSomething?

I can give some examples:

  1. The TStringStream inherits from TByteStream that inherits from TMemoryStream that finally inherits TCustomMemoryStream.
  2. TRESTClient directly inherits from TCustomRESTClient.
  3. TButton inherits from TCustomButtom.

There are indeed cases I see the actual benefit from the base TCustom... class, like in TMemo and TRichEdit, but I fail to understand why TRichEdit inherits from TCustomRichEdit instead of directly from TCustomMemo.


TL;DR: What's the use of the TCustomFoo classes in Delphi? When extending an existing class should I inherit from TCustomFoo or from TFoo directly?


Solution

  • You can inherit from either, but you'll often find that the TFoo class adds no functionality to the TCustomFoo class except for making protected properties public or published, and/or making public properties into published properties.

    You may want your own TAraujoFoo components to not have all those properties public/published, in which case you can inherit from TCustomFoo instead of TFoo, and just make public/published the properties that you want TAraujoFoo to have.