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 and methods.

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:
var
  W: integer;
begin
  W := Panel1.Width;
  Form1.Caption := 'The width of the panel is ' + IntToStr(W);
end;
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:
  Form1.Show;
  Label1.Hide;
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 
DelphiLand