In OOP languages, members of a class have a specific scope that indicates visibility. Standard visibility includes private, protected, and public. Private members are usable by the defining class only (fully encapsulated). They are invisible outside of the class except by friendly classes.
Protected members are usable by the defining class and descendant classes only (plus friendly classes). Public members are usable wherever its class can be referenced.
Languages Focus: Member Visibility
Traditional member visibility specifiers for fully OOP languages are private, protected, and public. Many modern OOP languages implement additional member visibilities.
Additional member modifiers are documented under the Member Modifiers topic.
Delphi Member Visibility
In Delphi, you group member declarations as part of defining the interface for a class in the Interface section of a unit.
Up until D2005, private and protected were not implemented strictly. Starting with D2005, a traditional strict versions of OOP are supported using the strict keyword. OO purist will want you to use strict private over private and strict protected over protected. I suggest you follow that advice until you both fully understand the differences and have a specific need.
Delphi offers a special published specifier which is the same as public members but runtime type information (RTTI) is generated.
TCyborg = class(System.Object) private //Don't use accept when you really want private friendly members. strict private //Use as your default private members.
FName: String; protected //Don't use accept when you really want protected friendly members.
strict protected //Use as your default protected members.
public
published
//RTTI Info
end;
Now let's dive deeper...
Class Scope Specifiers
Private, Protected, Public, and Published Members
In OOP languages, members of a class have a specific scope that indicates visibility (Class Visibility Specifiers). In OOP languages, private, protected, and public members are defined as follows:
Private = Used by the defining class only. Invisible outside of the class except by friendly classes.
Protected = Used by the defining class and descendant classes only (plus friendly classes).
Public = Visible wherever its class can be referenced.
Each OOP language implements these standard scope specifiers in slightly different ways. The following is a short description of Delphi's scope specifiers:
Private = Used by the defining class and classes defined in the same unit. Consider classes in the same unit as friendly.
Strict Private = Use the new strict visibility specifier which enforces the traditional definition of private members. With strict private, only the class that defines a private member can use the private member.
Protected = Same as private plus any descendant classes whether or not they are in the same unit.
Strict Protected = Use the new strict visibility specifier to enforce the traditional definition of protected members. With strict protected, classes defined in the same unit do not have access to the protected members that are not on its ancestor path.
Public = Visible wherever its class can be referenced.
Published = Same as public members but runtime type information (RTTI) is generated. RTTI allows an application to query the fields and properties of an object dynamically and to locate its methods. It's used to access properties when saving and loading form files, to display properties in the Object Inspector, and to associate event handlers with events.
Here is a traditional Delphi syntax example available since Delphi 1.0:
type
TPerson = class
private
protected
public
published
end;
Strict private and strict protected were introduced in Delphi 7 and available for Win32 with D2005:
type
TPerson = class
private
//Don't use accept when you really want private friendly members.
strict private
//Use as your default private members.
protected
//Don't use accept when you really want protected friendly members.
The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.