Access Modifiers

When declaring a field earlier in the chapter, you prefixed the field declaration with the keyword public. public is an access modifier that identifies the level of encapsulation associated with the member it decorates. Five access modifiers are available: public, private, protected, internal, and protected internal. This section considers the first two.

Beginner Topic: Encapsulation Part 2: Information Hiding

Besides wrapping data and methods together into a single unit, encapsulation is also about hiding the internal details of an object's data and behavior.

To some degree, methods do this; from outside a method, all that is visible to a caller is the method declaration. None of the internal implementation is visible. Object-oriented programming enables this further, however, by providing facilities for controlling the extent to which members are visible from outside the class. Members that are not visible outside the class are private members.

In object-oriented programming, encapsulation is the term for not only grouping data and behavior, but also hiding data within a class (the capsule) so that minimum access about the inner workings of a class is exposed outside the class.

The purpose of an access modifier is to provide encapsulation. By using public, you explicitly indicated that it is acceptable that the modified fields are accessible from outside the Employee classin other words, that they are accessible from the Program class, for example.

Consider an Employee class that includes a Password field, however. It should be possible to call an Employee object and verify the password using a Logon() method. It should not be possible, however, to access the Password field on an Employee object from outside the class.

To define a Password field as hidden and inaccessible from outside the containing class, you use the keyword private for the access modifier, in place of public (see Listing 5.15). As a result, the Password field is not intended for access from inside the Program class, for example.

Listing 5.15. Using the private Access Modifier

[View full width]

 class Employee {   public string FirstName;   public string LastName;   public string Salary;   private string Password;                                        private bool IsAuthenticated;                                   public bool Logon(string password)                              {                                                         if(Password == password)                                  {                                                              IsAuthenticated = true;                               }                                                     return IsAuthenticated;                              }                                                           public bool GetIsAuthenticated()                                                  {                                               return IsAuthenticated;                                                         }                                            // ...   }   class Program   {     static void Main()     {         Employee employee = new Employee();         employee.FirstName = "Inigo";         employee.LastName = "Montoya";              // ...        // Password is private, so it cannot be                                                           // accessed from outside the class.                                                          // Console.WriteLine(                                                                                        // ("Password = {0}", employee.Password);                                                                 }     // ...    } 

Although not shown in Listing 5.15, it is possible to decorate a method with an access modifier of private as well.

Note that if no access modifier is placed on a class member, the declaration will default to private. In other words, members are private by default and programmers need to specify explicitly that a member is to be public.

Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185 © 2008-2017.
If you may any questions please contact us: