Languages Focus: Base Class
When you create a class, it is either a base class or inherits from another class. Some languages require all classes to inherit from a common base class and some do not.
Delphi Base Class
In Delphi programming language (Object Pascal), all classes ultimately inherit from the base class TObject.
Syntax Example: //Specify both namespace and class:
TCyborg = class(System.TObject)
end;
//Use shortcut alias:
TCyborg = class(TObject)
end;
//None, default is System.TObject
TCyborg = class
end;
Fun With TObject
TObject has some basic functionality available for all Delphi classes. Since TObject is the base class for Delphi, you should become familiar with it's features.
To see TObject in action, alter the click event of a button as follows:
var
MyObject: TObject;
begin
MyObject := TObject.Create; //Create object instance.
ShowMessage(MyObject.ClassName); //Return the class name.
ShowMessage(UnitName); //UnitName is a class function
//defined in TObject.
MyObject.Free; //Free object instance.
end;
Next, view the source code for TObject to see what routines are implemented. The easiest way to find TObject's declaration is to right click on TObject in your source code and select Find Declaration. Most of these routines are documented in the help so you can place the cursor on any procedure or function name and press F1.
Common Inheritance Choices
The following is a short list of some more common VCL classes you can inherit from.
Class |
Use For |
Features / Notes |
TObject |
demos |
create, destroy |
TPersistent |
streaming objects |
Save object
TObject >> TPersistent |
TComponent |
non-visual classes |
Ability to appear on the Component / Tool palette and to be manipulated in the Form Designer.
TObject >> TPersistent >> TComponent |
TControl |
visual classes |
Visual elements like OnClick, OnDblClick, Top, Left, etc.
TObject >> TPersistent >> TComponent >> TControl |
IInterface |
Interfaces |
Base interface class. |
IUnknown |
Win 32 Specific Interfaces |
For Win32 specific interfaces. |
Step by Step Example
The following is a step-by-step tutorial on creating your first Delphi class and does include an inheritance example. From this simple tutorial, you can then experiment with the various class inheritance features.
The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.