100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Object Pascal Basics

Get started with Object Pascal's classes, methods, constructors, and inheritance — the object-oriented extension of standard Pascal.

Advanced PascalIntermediate10 min readJul 10, 2026
Analogies

Object Pascal Basics

Object Pascal extends standard Pascal with the class keyword, letting you bundle data (fields) and the operations on that data (methods) into a single reference type, along with support for inheritance, polymorphism, and encapsulation. Unlike records, which are value types copied on assignment, a class instance is a reference type: creating one with Create allocates it on the heap, and a variable of a class type holds a reference to that object rather than the object itself, similar in spirit to a pointer but managed through method calls instead of ^ syntax.

🏏

Cricket analogy: A class is like a player's complete franchise profile — batting average, team, jersey number (fields) plus the actions they can perform like BatShot or BowlDelivery (methods) — bundled as one reference that scouts pass around rather than copying the whole player each time.

Classes, Fields, and Methods

A class is declared with type TAnimal = class ... end;, with data fields listed directly and methods declared as procedure/function headers whose bodies are implemented separately using the dotted TAnimal.MethodName syntax, mirroring how a unit separates its interface from its implementation. Inside a method body, the implicit Self parameter refers to the specific object instance the method was called on, letting the same method code operate correctly on many different objects.

🏏

Cricket analogy: A method like TPlayer.ScoreRun operating on Self is like a generic 'take a single' technique that works correctly whichever specific batsman (object instance) is currently on strike.

pascal
type
  TAnimal = class
  private
    FName: string;
    FSound: string;
  public
    constructor Create(AName, ASound: string);
    procedure Speak;
    property Name: string read FName;
  end;

constructor TAnimal.Create(AName, ASound: string);
begin
  inherited Create;
  FName := AName;
  FSound := ASound;
end;

procedure TAnimal.Speak;
begin
  Writeln(FName, ' says ', FSound);
end;

var
  dog: TAnimal;
begin
  dog := TAnimal.Create('Rex', 'Woof');
  dog.Speak;
  dog.Free;
end.

Constructors, Destructors, and Inheritance

A constructor, declared with the constructor keyword (conventionally named Create), allocates and initializes a new object, while a destructor, declared with destructor (conventionally named Destroy), releases resources when the object is freed via Free. A class can inherit from another with TDog = class(TAnimal), gaining all its parent's fields and methods automatically, and can override a virtual method by redeclaring it with the override directive, provided the parent declared that method as virtual.

🏏

Cricket analogy: TDog inheriting from TAnimal is like an all-rounder profile inheriting the base 'Player' profile's name and team fields automatically, while overriding the base 'PlayShot' technique with its own specialized version.

Always call inherited as the first line of an overridden constructor (and typically the last action in an overridden destructor) so the parent class's own initialization or cleanup logic still runs — skipping it silently leaves inherited fields uninitialized or resources unreleased.

Visibility: private, protected, public

Object Pascal controls access to a class's members with visibility sections: private members are accessible only within the same unit as the class declaration, protected members are additionally accessible to descendant classes even in other units, and public members are accessible from anywhere the object is visible. A common pattern is to keep fields private (like FName) and expose controlled access through public properties (like property Name: string read FName write SetName;), which lets you validate or transform values on write without callers needing to know a setter exists.

🏏

Cricket analogy: Keeping FAverage private and exposing it via a public Average property is like a team keeping raw fitness-test data confidential internally while publishing only the polished 'fitness rating' to the media.

A method declared without virtual cannot truly be overridden — a descendant class declaring a method with the same name but without virtual/override merely hides the parent's method (static binding), meaning calls through a base-class-typed variable will still invoke the parent's version, not the descendant's, which is a common source of confusing polymorphism bugs.

  • A class bundles fields and methods into a reference type created on the heap with Create.
  • Methods are declared in the class body and implemented separately with dotted TClassName.MethodName syntax.
  • Self inside a method refers to the specific object instance the method was invoked on.
  • constructor Create initializes an object; destructor Destroy releases resources, invoked via Free.
  • Inheritance (class(Parent)) gives a descendant all parent fields/methods; virtual methods can be overridden.
  • Visibility sections (private, protected, public) control which code can access which members.
  • Properties expose controlled read/write access to private fields, often through validating methods.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PascalStudyNotes#ObjectPascalBasics#Object#Pascal#Classes#Fields#OOP#StudyNotes#SkillVeris