Sign in Join
Wiki copytemporary

Class

Deutsch · English · Français · Русский

A class is a highly structured data type in Object Pascal dialects such as Delphi or the ObjFPC dialect. Classes are able to contain variables, constructors, destructors, functions, procedures, and properties using access scopes.

Classes are able to inherit and to be inherited by other classes. For run-time purposes, any class not specifying a parent class automatically inherits from TObject, as it has required components for all classes. Because of TObject's dependency, any subclass's destructor must have the override directive. Additionally, any of your class's constructors must specify inherited in their body. A class can have several constructors, but only one destructor.

Object Pascal does not support multiple inheritance: apart from the implicit inheritance by TObject, classes can have only one ancestor class. Polymorphism is implemented with method directives. Here is a simple class declaration; let's explain it.

pascal
type
  TMyClass = class
  private
    FSomeVar: integer;
  public
    constructor Create; overload;
    constructor Create(Args: array of integer); overload;
    destructor Destroy; override;
    function GetSomeVar: integer;
    procedure SetSomeVar(newvalue: integer);
  published
    property SomeVar: integer read GetSomeVar write SetSomeVar default 0;
  end;

Between the keywords 'class' and 'end' we find member declarations, both variables and methods. Some methods (functions and/or procedures) are preceded by scope modifiers (private, public, published) and followed by directives (overload, override) and a strange property thing. Let's explain them all.

Notes on inheritance

In Object Pascal, derived classes inherit all members of the base class, even those not overloaded with the same name. Example:

pascal
type
  //base class
  MyClass = class
    procedure Proc1;   
  end;

  //derived class
  YourClass = class(MyClass)  
    procedure Proc1; //same name of MyClass procedure
  end;

var
  a: MyClass;
  b: YourClass;
begin
  a := MyClass.Create;
  b := YourClass.Create;
  a.Proc1;          // uses procedure in MyClass 
  b.Proc1;          // uses procedure in YourClass
  MyClass(b).Proc1; // uses procedure in MyClass

Scope modifiers

Scope modifiers tell the compiler who can call a method:

Scope modifiers cannot be changed in derived classes: members will maintain their visibility (or lack thereof) forever, everywhere.

Properties

A property is a variable that is accessed through methods. The variable SomeVar in the example above will be read and written in the code like a simple variable, but under the hood the compiler will call the methods specified in the property declaration. This allows to calculate it on the fly, or run boundary checks, input validation, formatting or just a placeholder for future extensions. Some rules apply:

default sets a starting value for the property: it's optional and can be omitted. Another option is stored false:

pascal
property SomeVar: Integer read GetSomeVar write SetSomeVar stored false;

This way the write method will be called, but the value will NOT be saved in the variable.

self

The self argument is passed by default to every (non static) method: it's an alias to the specific class instance which the method belongs to. This way every class can identify itself and have access to its members without ambiguity.

Method directives

Class methods can be changed, overloaded, made static and more. A method can have more than one directive. Directives rule the whole polymorphism system in Object Pascal; they can change the call model, too.

virtual, dynamic, override

these three directives are mutually exclusive. virtual means that the method can be overwritten by the derived class. dynamic means the same thing, but the implementation differs: virtual members addresses are stored in a table, while dynamic members do not use tables and do not occupy RAM, but their resolution mechanism is slower.

A derived class can implement its own version of a virtual method, but the base method is still available; if the new method is marked override it hides the base virtual method, which cannot be called anymore. This holds true in case of dynamic methods, too. However you cannot override a method if it's not virtual or dynamic.

static

A static method or variable is common to every instance of the class; one class instance can write a static variable member and every other instance will retain (and read, if accessed) the new value. For this reason, the compiler does NOT pass the 'self' parameter to static methods: it would be nonsense. Moreover, static methods cannot be virtual (but see class methods below).

abstract

A method declared abstract is declared, but not implemented in the base class. Derived classes will be forced to provide their own implementation.

overload

pascal
function MultiSum(a, b: integer): integer; overload;
function MultiSum(a, b: AnsiString): AnsiString; overload;

With the overload directive you can declare many methods - functions or procedures - with the same name but different type and parameters.

The class needs the constructor to be implemented in the implementation section of the unit:

pascal
implementation

constructor TMyClass.Create;
begin
  inherited;
  SomeVar := 6;
end;

end.

The destructor and all procedures and functions of the class must also be implemented in the implementation section.

Instances of the class can then be declared as variables:

pascal
var
  classInstance: TMyClass;

Before using an instance, it must be created with:

pascal
begin
  classInstance := TMyClass.Create;
  ...
end;

Classes compared to other structured types

See also

extended class syntax Examples


From the Free Pascal wiki, page "Class", as it stood on 2022-02-12 after a change by Slawek, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.