Reference & Resources

OOP in Delphi - Object - Class

In Object Oriented Programming (OOP), you write programs that manipulate objects. An object is a combination of data and program code; it is created from a class (a kind of template, blueprint).

You could think of "dog" as a class and your particular dog as an object, an instance of the class "dog".
Delphi's VCL (Visual Component Library) is a collection of classes to create objects such as forms, tables, queries, radio buttons, check boxes, etc.

Object and Class

An object is an instance of a class, it's also called a "class object".
In general, the word instance means: a particular occurrence (case, specimen, example, sample) of something. In OOP, instance is synonymous with "object".

Objects have properties, methods and events.

A property defines an attribute of an object. Under the hood, a property is in fact a subroutine that gets or sets a hidden "field" of the object, but you can work with it just as with a variable. Examples:
  W: integer;
  W := Panel1.Width;
  Form1.Caption := 'The width of the panel is ' + IntToStr(W);
Note that a property can also be an object, for example: Form1.Canvas.

An object method is a subroutine that acts on an object. Examples:
In Delphi, almost all the code you write is executed, directly or indirectly, in response to an event. An event is a special kind of property that represents a run-time occurrence, often a user action, such as clicking a button or pressing a mouse button. The code that responds directly to an event - called an event handler - is a Delphi procedure.

To create an event handler for a component:

  1. Select a component.
  2. Click the Events tab in the Object Inspector. The Events page of the Object Inspector displays all the published events defined for the component.
  3. Select the event you want, and then either double-click the Value column or press Ctrl+Enter. The Code Editor opens with the cursor inside the skeleton of the event handler.
  4. At the cursor, type the code that you want to execute when the event occurs.

Some events are: OnClick, OnCreate, OnDblClick, OnDestroy, OnEnter, OnExit,... Example of an event handler:

procedure TForm1.Button1Click(Sender: TObject);
  //Code to be executed
  Label1.Caption := Edit1.Text;
The creation of an object is called instantiation. Instantiation is also known as "construction".
An object is created from a class by a subroutine called a constructor, and it's destroyed by a destructor.

Very important!
Each object that was created, must be destroyed before the end of the program, in order to free the memory that it occupied. VCL components that were created by Delphi will be destroyed automatically, but if you created an object yourself, you have to destroy ("free") it explicitly. For example:
var MyDog: TDog;
MyDog := TDog.Create;
MyDog.Free; // before the program ends


OOP principles

Object Oriented Programming is based on 4 principles:

  1. Abstraction: only "relevant" stuff is shown and the unnecessary details of an object are hidden from the user.
  2. Encapsulation: keeps the internal data and code safe from external manipulation. The user only knows how to use an object, but the actual implementation, like how and where the data is actually stored, is hidden.
  3. Inheritance: each class inherits some or all the properties and methods of another class, its "ancestor". In Delphi, this is specified in a "type declaration", such as this one that you'll see in the unit for a form:
    type TForm1 = class(TForm)
    meaning: TForm1 inherits all from the class TForm that is predefined in the VCL, except for the properties that you customized, such as maybe its Caption, Color, Height, Width, and so on.
  4. Polymorphism: objects of different types can be accessed through the same interface. Different objects can respond to identical messages, each in its own way.
    Example: Form1.Show and ListBox1.Show both display an object on the screen, but the underlying code is very different. Thru' encapsulation, the programmer doesn't have to worry how the method Show is performed.


OOP in Delphi
Table of contents

1. Object - Class
2. TForm objects
3. Hierarchy of classes

Reference  Crash Course Delphi  FAQ
Tips  Source Code  Downloads  Links

Copyright 1999-2018