IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Member Visibility
 
Posted 11 years ago on 11/4/2008 and updated 2/4/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page

KB101490



General Info: Class Visibility Specifiers

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.

Syntax Example:
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.
strict protected
    //Use as your default protected members.
public
  
  published
  
  end;

 

More Info

Article:  A 10 Minute Your First Delphi Class Quick Start
Definition:  Class Visibility Specifiers

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1257A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

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.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

The class visibility specifiers are:

Answer:
1. 

private, strict private, protected, stict protected, public, and published.

2. 

private, protected, public, and published.

3. 

private, protected, and public.

4. 

private, protected, assembly and protected, assembly or protected, and public.

5. 

private, protected, internal, protected internal, and public.


 KB Article #101490 Counter
35563
Since 11/4/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]