ObjectPascal
Public
Directive
Starts an externally accessible section of a class
  type Class declaration
  Public
    Field | Property | Method declaration
  {...}
  end;
Description
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.
Notes
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
 
Example code : A sublass 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;

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 TChnageableSquare 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;

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

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

  // Change the square dimensions
  mySquare.ChangeSize(10,20);

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

end.
   Square 30,40 area = 1200
   Square 10,20 area = 200