Introducing Access Control

As you know, encapsulation links data with the code that manipulates it. However encapsulation provides another important attribute:  access control. Through ' encapsulation, you can control what parts of a program can access ,the members of a class. By controlling access, you can prevent misuse. For example, allowing access to data only through a well-defined set of methods, you can prevent the misuse of that   data. Thus, when correctly implemented, a class creates a "black box" which may be' used, but the inner workings of which are not open to tampering. However, the classes, that were presented earlier do not completely meet this goal. For example, consider the Stack class shown at the end of Chapter 6. While it is true that the methods push( ) and-. pop( ) do provide a controlled interface to the stack, this Interface is not enforced. That is, it is possible for another part of 'the program to bypass these methods and access the stack directly. Of course, in the wrong hands, this could lead trouble, In this section you will be introduced to the mechanism by which you can precisely control access to
the various members of a Class.

How a member can be accessed is determined by the access specifier that modifies its declaration. Java supplies a rich set of access specifiers. Some aspects of access control are related mostly to inheritance or packages. (A package is, essentially, a grouping of. classes.) These parts of Java's access control mechanism will be discussed later. He begin by examining access control as it applies to a single class. Once you, understand the fundamentals of access control, the rest will be easy. Java's access specifiers are public, private, and protected. Java also defines a default access level. protected applies only  is involved. The other access specifiers are described next. Let's begin by public and private. When a-member of a class is modified' , ,by the public specifier, t!ten that member can be accessed by any other code 'in your'  program. When a member of a class is specified as private, then that member can only' ,  other members of its class. Now you can understand why main.

'always been preceded by the public spectacle. It is called by code that is outside the program-that is, by the Java run-time system. When no access specifier is used, then ' by default the member of a class is public within its own package, but cannot be : accessed outside of its package. (Packages are discussed in the following chapter.) ', In the classes developed so far, all members of a.class have used the default access , mode, which is essentially public. However, this is not what you will typically want to, be the case. Usually, you will want to restrict access to the data members of a . class-allowing access only through methods. Also, there will be times when you will want to define methods, which are private to a class.

As you can see, inside the Test class, a uses' default access, which 'for this example is the same as specifying public. b is explicitly specified as public. Member c is given private access. This means that it cannot be accessed by code outside of its class. So, inside. the. Access Test class,c cannot be used directly. It must be accessed through its public  methods: and  If you were. to remove the comment symbol from the
beginning of the following line,

As you can see, now both stick, which holds the stack, and which is the index of the .top of the 'stack, are specified as private. This. means that they cannot be accessed or· , altered except through  and pop  Making t05 private, for example, prevents other parts of your program from inadvertently setting  a value that is beyond the. end of the  The following program demonstrates he' improved' Stack class, Try removing the containment-out lines to prove to yourself that the and t05 members are, indeed, inaccessible.

Share This