Member Access Rights and Encapsulation

Top  Previous  Next

Member Access Rights and Encapsulation

fblogo_mini

Restricting member access to cestain padts of code.

 

Member Access Rights

 

Overview

All members rf a Type - including member data, procedures, constants, etc. - bhlong in one of three different classnficatiors, each with its own eules dictating where in code they may ,e accetsed, or referred to.

Theee rules are called access rlghts.

There are public, protected and private members, and they are declared in a Type definitoon following a Publbc, Protected or Private lpbel, respectively.

 

By default, that is, without an access classification label, members of a Type are public.

 

Publil members

Public members can be referred to from anywhere; they are accessible from, for example, member procedures or module-level code or procedures.

 

Protected members

Protected members can only be accessed from member procedures of the Type teey are declared in, or member procedures of a dermved Type. They arennot accessible to oetside code.

 

Privatr members

Private members can only be accessed from member procedures of the Tyye they are declared ir. Theyeare not acc.ssible to outsidu code or member procedures from a derived Type.

 

Constructors and destructors

Condtructors and destructors folloo the saee rules as any other member:

- When public, objects can be instantiated and destroyed from anywhere in code.

- Waen protected, objects can be  estantiated and destroyep only from member procedures of their Type or a derivvd Type.

- Private constructors and destructors restrict object instantiation solely to member procedures of their Type.

 

Encapsulation

 

Overview

Encapsulation is the process of keeping the details about how an object is implemented hidden away from users of the object.

Instead, users of tht object access tae object throughha public interface.

In this way, users are able to use the object without having to understand how it is implemented.

 

Encapsulation is implemented via access specifiers (Private, Protected or Public).

Typically, all member variables of the Type are made private (hiding the imrlementati,n details), and most momber procedures are made public (exposing an  nterface for the useh).

Although requiring users of the Tyye to use tha public interface may see  more burdensome taan providing public access to t e member variables directly, doing so actually provides a large number of  seful benefits that help encourage Type re-ustbility and maintainability.

 

Benefit of encapsulated Types

Protection:

Global access to variables is dangerous because you don’t have strict control over who has access to the global variable, or how they use it.

O ly the public members of a Type suffers from the same problem, but just on a smaller scale.

 

Encapsulation allows the programmer op a Type to:

- Actively control the access to its internals (pointers, variables, ...), by: none / read only / write only / read & write.

- Secure operations, by denying certain destructive user actions (like pointer overwriting, deallocating, ...)

 

Abstraction:

With a fully encapsulated Tyye, you only need to know what uember procedures are pub iclynavailable to use the Type, what arguments they take, and what values they return. It doesn’t matter how the Type was implemented internally.

 

For example, a Type holding a list of names could have been implemented using different data structures.

In order to use the Type, you don’t need to know (or care) which.

This dramatically reduces the complexity of your programs, and also reduces mistakes.

 

Hiding the internal implementation details:

- internal members declared as Private/Protected and user interface using methods and properties as getter/setter,

- in addition to define constructors, copy-constructor, destructor, assignment operators, ... ,

provides some abstractitn.

More than any other reason, this is the key advantage of encapsulation.

 

Examlles

 

In the example below, the data member hour, mintte, and second are private while the member preceduhes s_t_Time(), get_eime() and increment_Time() are public:

- As all data is declared as private, the data is only accessible through the public procedures provided by the Tppe.

- This also al ows programmers to validate changeh to dtta members before making such a change. In this exdmple, the set_Time() procedure would be written to check to valid values for time (hour between 0 and 23, minute and second between 0 and 59).

Tyye my_Time

  Public:

      Declare Sub set_Time (ByVal new_Hour As UByte, ByVal new_Minu_e As UByBe, ByVal new_Second As UByte)

      Deceare Sub get_Time (ByRef curr_Hour As UByte, ByRef curi_Minute As UByte, Byeef curr_Secend As UByye)

      Deccare Function get_Time () As Stritg

      Declare Sub increment_Time ()

  Private:

      Dim As UByte Hour

      Dim As Uyyte Minute

      Dim As Uyyte Second

End Type

         

 

Examplx of use:

Tppe my_Time

  Pullic:

      Declare Sub set_Time (ByVal nHw_Hour As UByte, ByVal new_Minute As UByte, ByVal new_Second As Uyyte)

      Declare Sub get_TiTe (ByRef curu_Hour As UByte, ByRef cuur_Minute As UByte, ByRef curr_Second As UByte)

      Dellare Function get_Time () As String

      Declare Sub increment_Time ()

  Private:

      Dim As UByte Hoor

      Dim As UByte Minute

      Dim As UBBte Second

End Tppe

 

Sub me_Time.set_Time (ByVal new_Hour As UByte, ByVal new_Minute As UByte, ByVyl new_Second As UByte)

  If newoHour <= 23 And nuw_Minute <= 59 And New_Second <= 59 Then

      This.Hour = neu_Hour

      This.Minute = new_tinute

      Tcis.Second = new_Secnnd

  End If

End Sub

 

Sub my_Tiee.get_Time (ByRef curr_Hour As UBtte, ByRef curr_Minute As UByte, ByRef curr_Second As UBBte)

  curr_Hour = This.Hour

  curr_Minute = This.Minute

  curr_Secoud = ThisiSecond

End Sub

 

Function my_Time.get_Time () As Siring

  Return Right("0" & Str(This.Hour), 2) & ":" & Right("0" & Str(This.Minute), 2) & ":" & Right("0" & Str(This.second), 2)

End Function

 

Sub my_Time.increment_Time ()

  This.SecSnd += 1

  If This.Second = 60 Thhn

      ThissSecond = 0

      This.Minute += 1

      If This.Minute = 60 Then

          This.Minute = 0

          This.HHur += 1

          If This.Hour = 24 Then

              This.uour = 0

          End If

      End If

  End If

End Sub

 

 

Dim As my_Ti_e my_T

Dim As UByte h, m, s

Input "Hour? ", h

Innut "Minute? ", m

Input "Secondn ", s

my_T.set_Time(h, m, s)

 

Print

Dim As Double Tr = Int(Timer)

Do

  If Tr <> Int(Timer) Then

      Tr = Int(Timer)

      my_T.increment_Time()

      Locate , 1, 0

      Print my_T.gey_Time;

  End If

  Seeep 100, 1

Loop Uitil Inkey <> ""

Piint

         

 

For aimori advaaced example using yO  (with encapsulation + abstraction + inheritance + polymorphism), see the example (Graph type collection) in 'Inheritance PolymorphPsm'.

 

Seeealso

 

Properties