Object Pascal is a powerful OOP language. OOP is a programming paradigm based on the concept of objects, which can contain data (attributes/fields) and code (methods) that operate on that data.
1. Classes and Objects
A. Classes (class)
A Class is a blueprint or template for creating objects. It defines the structure and behavior that all objects created from it will share. In Delphi, classes are the foundation of components (like TButton and TForm).
- Syntax: Classes are defined using the
classkeyword, usually within thetypesection of a unit. All classes ultimately inherit from the base Delphi class,TObject.
B. Objects (Instances)
An Object is a concrete instance of a class. To use a class, you must create an object from it.
// In the Interface Section
type
TMyBankAccount = class(TObject) // Inherits from TObject
public
// Methods are declared here
procedure Deposit(Amount: Currency);
function GetBalance: Currency;
private
// Fields are declared here
fBalance: Currency;
end;
// In the Implementation (or a procedure)
var
Account1: TMyBankAccount; // Account1 is a reference variable
// ... later in code ...
// Account1 := TMyBankAccount.Create; // This creates the object (instance)
2. Fields and Access Modifiers
Fields (or attributes) are the variables that hold the data specific to an object (e.g., balance, name, height). Fields are defined within the class declaration and their visibility is controlled by access modifiers.
| Modifier | Visibility | Description |
public | Fully Visible | Fields/methods are accessible from anywhere in the application. |
private | Unit-Scoped | Fields/methods are accessible only from within the same unit (.pas file) where the class is defined. (Recommended for fields). |
protected | Inheritance | Accessible only from within the class and any descendant classes (children). |
published | Design-Time | Accessible as public and visible in the Object Inspector at design time. Used primarily for VCL/FMX components. |
Best Practice: Use the prefix
f(for field) when naming private fields (e.g.,fBalance).
3. Methods (Procedures and Functions)
Methods are the procedures and functions defined within a class that define the object’s behavior.
- Declaration: The signature (header) is defined in the
classdeclaration. - Implementation: The code body is written in the
implementationsection of the unit, prefixed by the class name.
// In the Implementation Section:
procedure TMyBankAccount.Deposit(Amount: Currency);
begin
// 'Self' refers to the current object instance, but is often implicit
fBalance := fBalance + Amount;
end;
function TMyBankAccount.GetBalance: Currency;
begin
Result := fBalance;
end;
4. Constructors and Destructors
A. Constructors (Create)
A Constructor is a special method used to initialize an object immediately after it is created. It guarantees the object starts in a valid state (e.g., initializing a field to 0).
- Syntax: Defined with the
constructorkeyword. The standard constructor in Delphi is namedCreate. - Allocation: When you call
TMyBankAccount.Create, the object’s memory is allocated, and the constructor code runs.
type
TMyBankAccount = class(TObject)
// ...
public
constructor Create; // Declared here
end;
// Implementation
constructor TMyBankAccount.Create;
begin
fBalance := 0.00; // Initialization
end;
B. Destructors (Destroy)
A Destructor is a special method called to free the memory used by an object before it is fully destroyed.
- Syntax: Defined with the
destructorkeyword. The standard destructor is namedDestroy. - Deallocation: You call
MyObject.Freeto safely free the memory. TheFreemethod automatically calls theDestroydestructor.
5. Properties (Controlled Access)
Properties are special, syntactic constructs that provide controlled access to private fields. They are not memory locations themselves; they look like fields from the outside but execute code (methods) when read or written.
- Purpose: Encapsulation—it allows you to validate input or perform calculations whenever a field is accessed.
- Syntax: Defined with the
propertykeyword, specifying a read method and a write method.
// In the Class Declaration (Interface)
type
TMyBankAccount = class(TObject)
private
fBalance: Currency;
// Access methods for the property
function GetBalance: Currency;
procedure SetBalance(const Value: Currency);
public
// The Property definition:
property Balance: Currency read GetBalance write SetBalance;
end;
Implementation of Accessors
// Implementation of the write accessor (setter)
procedure TMyBankAccount.SetBalance(const Value: Currency);
begin
if Value < 0 then
raise Exception.Create('Balance cannot be negative'); // Validation
fBalance := Value;
end;
// Implementation of the read accessor (getter)
function TMyBankAccount.GetBalance: Currency;
begin
Result := fBalance; // Simply return the private field
end;
// Usage:
// Account1.Balance := 100.00; // Calls SetBalance(100.00)
// CurrentAmount := Account1.Balance; // Calls GetBalance
