Show: Delphi C++
Display Preferences

Private, Protected, Public, and Published Declarations

From RAD Studio
Jump to: navigation, search

Go Up to Using the object model Index

A class type declaration contains three or four possible sections that control the accessibility of its fields and methods:

Type
  TClassName = Class(TObject)
    public
      {public fields}
      {public methods}
    protected
      {protected fields}
      {protected methods}
    private
      {private fields}
      {private methods}
end;
  • The public section declares fields and methods with no access restrictions. Class instances and descendent classes can access these fields and methods. A public member is accessible from wherever the class it belongs to is accessible - that is, from the unit where the class is declared and from any unit that uses that unit.
  • The protected section includes fields and methods with some access restrictions. A protected member is accessible within the unit where its class is declared and by any descendent class, regardless of the descendent class's unit.
  • The private section declares fields and methods that have rigorous access restrictions. A private member is accessible only within the unit where it is declared. Private members are often used in a class to implement other (public or published) methods and properties.
  • For classes that descend from TPersistent, a published section declares properties and events that are available at design time. A published member has the same visibility as a public member, but the compiler generates runtime type information for published members. Published properties appear in the Object Inspector at design time.

When you declare a field, property, or method, the new member is added to one of these four sections, which gives it its visibility: private, protected, public, or published.

See Also

Personal tools
In other languages