Description |
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 |
Function |
|
Defines a subroutine that returns a value |
Private |
|
Starts the section of private data and methods in a class |
Procedure |
|
Defines a subroutine that does not return a value |
Property |
|
Defines controlled access to class fields |
Public |
|
Starts an externally accessible section of a class |
Published |
|
Starts a published externally accessible section of a class |
Type |
|
Defines a new category of variable or process |
|
|
|
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;
interface
uses
Forms, Dialogs, Classes, Controls, StdCtrls, SysUtils;
type // 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);
end;
// Define a descendant type : // It must use the parent protected method to set the // private area of the square
TChangeableSquare = class(TSquare)
public
procedure ChangeSize(newWidth, newHeight : Integer);
end;
// Define the form class used by this unit
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm} // Include form definitions
// Create the TSquare object
constructor TSquare.Create(width, height: Integer);
begin // Save the width and height in protected fields
squareWidth := width;
squareHeight := height;
// And calculate the square area
setArea;
end;
// Change the TSquare dimensions
procedure TChangeableSquare.ChangeSize(newWidth, newHeight: Integer);
begin // Over-write the original width and height values
squareWidth := newWidth;
squareHeight := newHeight;
// And re-calculate the square area
setArea;
end;
// Set the square size from its dimensions
procedure TSquare.setArea;
begin // Calculate the square area and store privately
squareArea := width * height;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
mySquare : TChangeableSquare;
begin // 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
mySquare.ChangeSize(10,20);
// What is the square area now?
ShowMessage('Square 10,20 area = '+IntToStr(mySquare.area));
end;
end.
|
Square 30,40 area = 1200
Square 10,20 area = 200
|
|