Last Updated:

Object and class | Dephi | Tutorial

Before you begin, you must enter the basic concepts and definitions.

A class in Object Pascal is a language structure that can include variables, functions, and procedures. Variables, depending on their purpose, are called fields or properties (see below). The procedures and functions of the class are methods. The type corresponding to the class will be called an object type:

type
TMyObject = class(TObject)
MyField: Integer;
function MyMethod: Integer;
end;

This example describes the TMyObject class, which contains the Field MyField and the MyMethod method.

Object fields are similar to record fields. This data is unique to each instance of the class that you create in your program. The TMyObject class described here has one field, MyField.

Methods are procedures and functions that are described within a class and are intended for operations on its fields. The class includes a pointer to a special table that contains all the information needed to call methods. Methods differ from conventional procedures and functions in that they are given a pointer to the object that called them. Therefore, the fields of the object that called the method will be processed. Inside the method, a pointer to the object that called it is available under the reserved name self.

The concept of a property will be discussed in detail below. For now, you can define it as a field that can be read and written not directly, but through the appropriate methods.

Classes can be described either in the module interface section or at the top level of the nesting of the implementation section. It is not allowed to describe classes "anywhere", that is, inside procedures and other code blocks.

Advance class declaration is allowed, as in the following example:

type
TFirstObject = class;
TSecondObject = class(TObject)
Fist: TFirstObject;
…
end;
TFirstObject = class(TObject)
…
end;

To use a class in your program, you must at least declare a variable of that type. An object-type variable is called a class instance or object:

var
AMyObject: TMyObject;

Before the introduction of the term "class" in the Pascal language, there was an ambiguity of the definition of "object", which could denote both a type and a variable of this type. Now there is a clear boundary: a class is a description, an object is something that is created in accordance with this description.

How are objects created and destroyed?

Those who have previously used OOP in C++ and especially in Turbo Pascal, be careful: in Object Pascal, object instances can only be dynamic. This means that in the above fragment, the AMyObject variable is actually a pointer containing the address of the object.

The object "comes to light" as a result of calling a special method that initializes the object - the constructor. The created instance is destroyed by another method – the destructor:

AMyObject: = TMyObject.Create;
{ actions with the created object}
…
AMyObject.Destroy;

But, the attentive reader will say, since the object does not yet exist, how can we call its methods? Fair point. Note, however, that the TMyObject.Create method is called, not AMyObject.Create. There are methods (including a constructor) that work successfully before (or even without) creating an object. Such methods, called class methods, will be discussed below.

In Object Pascal, a class can have multiple constructors. It is common to call the constructor create (as opposed to Turbo Pascal, where the constructor was usually called init, and from C++, where its name is the same as the class name). The typical name of the destructor is Destroy.

type
TMyObject = class(TObject)
MyField: Integer;
Constructor Create;
Destructor Destroy;
Function MyMethod: Integer;
end;

To destroy an instance of an object, it is recommended that you use the Free method, which initially checks the pointer (whether it is equal to Nil) and only then calls Destroy:

AMyObject.Free;

Before the control is transferred to the body of the constructor, the object is actually created - memory is allocated for it, the values of all fields are reset. Next, the constructor code written by the programmer to initialize instances of this class is executed. Thus, although at first glance the syntax of the constructor is similar to a call to a procedure (no return value is defined), the constructor is actually a function that returns a created and initialized object.

Note
The constructor creates a new object only if its name is preceded by the name of the class. If you specify the name of an existing object, it behaves differently: it does not create a new object, but only executes the code contained in the constructor body
.

To properly initialize fields related to the ancestor class in the object that you are creating, you must immediately call the ancestor constructor using the inherited reserved word when you enter the constructor:

constructor TMyObject.Create;
begin
inherited Create;
…
end;

Taking any of the examples included with this book or shipped together in Delphi, you will see almost no calls to constructors and destructors. The fact is that any component that appears in your application from the Component Palette during visual design is included in a certain hierarchy. This hierarchy is closed on the form (TForm class): for all its component parts, constructors and destructors are called automatically, invisible to the programmer. Who creates and destroys forms? This is done by an application (a global object named Application). In the project file (.dpr), you can see calls to the Application.CreateForm method for this purpose.

As for objects that are created dynamically (at the time of application execution), you need an explicit call to the constructor and the Free method.