Quick start manual
Classes and objects
7-3
Class types
Inheritance and scope
When you declare a class, you can specify its immediate ancestor. For example,
type TSomeControl = class(TControl);
declares a class called TSomeControl that descends from TControl. A class type
automatically inherits all of the members from its immediate ancestor. Each class can
declare new members and can redefine inherited ones, but a class cannot remove
members defined in an ancestor. Hence TSomeControl contains all of the members
defined in TControl and in each of TControl‘s ancestors.
The scope of a member’s identifier starts at the point where the member is declared,
continues to the end of the class declaration, and extends over all descendants of the
class and the blocks of all methods defined in the class and its descendants.
TObject and TClass
The TObject class, declared in the System unit, is the ultimate ancestor of all other
classes. TObject defines only a handful of methods, including a basic constructor and
destructor. In addition to TObject, the System unit declares the class-reference type
TClass:
TClass = class of TObject;
For more information about TObject, see the online Help. For more information about
class-reference types, see “Class references” on page 7-24.
If the declaration of a class type doesn’t specify an ancestor, the class inherits directly
from TObject. Thus
type TMyClass = class
ƒ
end;
is equivalent to
type TMyClass = class(TObject)
ƒ
end;
The latter form is recommended for readability.
Compatibility of class types
A class type is assignment-compatible with its ancestors. Hence a variable of a class
type can reference an instance of any descendant type. For example, given the
declarations
type
TFigure = class(TObject);
TRectangle = class(TFigure);
TSquare = class(TRectangle);
var
Fig: TFigure;
the variable Fig can be assigned values of type TFigure, TRectangle, and TSquare.