Classes, objects, encapsulation, inheritance and polymorphism — the heart of the Grade 12 practical paper and almost always the biggest single question.
A class bundles data (fields) with the code that operates on it (methods). Objects are references: you must Create them, and — unlike Java — you must Free them, because Pascal has no garbage collector.
Example
type TLearner = class private FName: String; FMark: Integer; procedure SetMark(value: Integer); public constructor Create(AName: String); function ToStringValue: String; property Name: String read FName; property Mark: Integer read FMark write SetMark; end; constructor TLearner.Create(AName: String); begin FName := AName; FMark := 0; end; procedure TLearner.SetMark(value: Integer); begin if (value >= 0) and (value <= 100) then FMark := value; // reject impossible marks end; var l: TLearner; begin l := TLearner.Create('Kabelo'); try l.Mark := 87; WriteLn(l.ToStringValue); finally l.Free; end; end.
| Concept | In Delphi |
|---|---|
| Encapsulation | private fields + public properties; validate on write in the setter |
| Constructor | constructor Create(...) — called as TClass.Create |
| Inheritance | TChild = class(TParent); call the parent with `inherited` |
| Polymorphism | virtual in the parent, override in the child |
| Abstract | virtual; abstract; — no body, every child must supply one |
💡 Exam Tip
Papers frequently give you a class with the methods stubbed out and ask you to complete them. Read the class declaration first: it tells you exactly what each method must return.