Starts a section of class private data accesible to sub-classes
type Class declaration
  Field | Property | Method declaration
The Protected directive starts a declaration section of a class definition. In a protected section, Fields, Properties and Methods are declared to be accessible to this class and classes descending from it. But not accessible externally by class users.
It is similar to the Private directive - it hides the internal implementation of a class, but does not hide such data and methods from subclasses.
In general, most data and methods internal to a class should be defined in a Protected section. This gives subclasses often useful access to these. Only use Private when you are sure that you want to keep stuff entirely local to the current class/unit. This may be true when a subclass would want to be shielded from parent class complexities.
You might want to make protected methods virtual to allow subclasses to alter them to suit their needs.
Related commands
FunctionDefines a subroutine that returns a value
PrivateStarts the section of private data and methods in a class
ProcedureDefines a subroutine that does not return a value
PropertyDefines controlled access to class fields
PublicStarts an externally accessible section of a class
PublishedStarts a published externally accessible section of a class
TypeDefines a new category of variable or process
 Download this web site as a Windows program.

Example code : A sublass that uses a protected method to access private data in the parent clas
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;


  Forms, Dialogs, Classes, Controls, StdCtrls, SysUtils;

  // Define a base TSquare class :
  // It has private data that can only be set by a protected method
  TSquare = class
  private          // Only known to the parent class
    squareArea : Integer;
  Protected        // Known to all classes in the hierarachy
    squareWidth, squareHeight : Integer;
    procedure setArea;
  public            // Known externally by class users
    property width  : Integer read squareWidth;
    property height : Integer read squareHeight;
    property area   : Integer read squareArea;
  published        // Known externally : has run time info also
    constructor Create(width, height : Integer);

  // Define a descendant type :
  // It must use the parent protected method to set the
  // private area of the square
  TChangeableSquare = class(TSquare)
    procedure ChangeSize(newWidth, newHeight : Integer);

  // Define the form class used by this unit
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;

{$R *.dfm} // Include form definitions

// Create the TSquare object
constructor TSquare.Create(width, height: Integer);
  // Save the width and height in protected fields
  squareWidth  := width;
  squareHeight := height;

  // And calculate the square area

// Change the TSquare dimensions
procedure TChangeableSquare.ChangeSize(newWidth, newHeight: Integer);
  // Over-write the original width and height values
  squareWidth  := newWidth;
  squareHeight := newHeight;

  // And re-calculate the square area

// Set the square size from its dimensions
procedure TSquare.setArea;
  // Calculate the square area and store privately
  squareArea := width * height;

procedure TForm1.FormCreate(Sender: TObject);
  mySquare : TChangeableSquare;
  // Create a changeable square
  mySquare := TChangeableSquare.Create(30, 40);

  // What is the square area now?
  ShowMessage('Square 30,40 area = '+IntToStr(mySquare.area));

  // Change the square dimensions

  // What is the square area now?
  ShowMessage('Square 10,20 area = '+IntToStr(mySquare.area));

  Square 30,40 area = 1200
  Square 10,20 area = 200
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page