Starts an externally accessible section of a class
  type Class declaration
    Field | Property | Method declaration
The Public directive starts a declaration section of a class definition. In a public section, Fields, Properties and Methods are declared to be accessible to this class, classes descending from it, and code using object instances of the class.
In Object Oriented terms, a class object is seen as a black box. The internal operations are not relevant. Public fields, properties and methods are the externally visible part of an object - a controlled access to the internals of the class.
Because public acceess provides a linkage of sorts to external code, you should avoid wherever possible making changes to the public section.
A Published section is very similar to a Public section, except that it provides some run time information.
Warning : avoid making fields public - it is always better to define a property to access them instead. This provides some decoupling from the internals of the class.
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
Protected Starts a section of class private data accesible to sub-classes
Published Starts a published externally accessible section of a class
Type Defines a new category of variable or process
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : A subclass that uses a public method to access internal data and methods
// 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 TChnageableSquare 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;

// Main line code
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 2002 - 2016. All rights reserved.  |  Home Page