3.11 ACCESS CONTROL FOR CLASS MEMBERS


3.11 ACCESS CONTROL FOR CLASS MEMBERS

Every member of a class has associated with it an access control property. In C++, a member can be private, protected, or public. In addition to these three, Java also allows for the access control property of a member to be package. When the access control modifier is left unspecified for a class member in Java, it is of type package.

Before explaining what each of these access control modifiers means, let's first address the issue of why any access control is needed at all. People just getting into OO programming frequently ask: Suppose all of the source code was made available to an application developer, would access control still make sense? If you somehow know or find out about a private data member in a class, what's so private about it?

The basic reason for access control is not to hide the information from the users of the software. On the other hand, it is to prevent direct alteration of a data member or direct invocation of a member function. Sometimes changing a data member entails that the state of the object be changed in other ways also. For example, if you write a window manager program for orchestrating the desktop environment on a workstation, you would not want some other program (or a human agent) to directly change the value of, say, the window_width variable. Changing the width of a window might, for example, also require that any images displayed in the window be resized so that they don't look distorted. So you'd want there to exist some kind of a setWindowWidth function that will take care of all of those additional details as you alter the width of a window. This you could accomplish by making the width data member private to a Window class and then supplying a public setWindowWidth function.

So access control is simply a means to regulate how the state of an object can be changed, making it less likely that when the program is extended by an application developer that it will behave in ways not intended originally.

Access Control in C++

All members of a C++ class are private unless declared explicitly to be otherwise.

As the name implies, the public members of a class are accessible to all other classes and functions. Additionally, the public members of a class are inherited by its subclasses.

On the other hand, members that are private to a class are accessible only to the definitions that are meant specifically for that class. Private members of a class are also accessible to the friends of that class. (We will shortly show an example to illustrate the notion of a friend in C++.) While the private members of a class are inherited by its subclasses, they cannot be accessed within the subclasses.

The access control modifier protected is used for a member if we wish for that member to be accessible to only the subclasses of that class. In other words, a protected member of a class acts like a public member for the subclasses derived from that class, but like a private member for the rest of the program. We will have more to say about this when we discuss how to extend classes in Chapter 15.

As we mentioned already, the private members of a class are accessible to the friends of that class. The following example illustrates this concept (which does not have a parallel in Java).

The data members m and n are private to the class X. But note that another class Y and a global function print() are declared to be the friends of class X in lines (B) and (C). (So that the identifier Y would make sense to the compiler in line (B), we had to declare the class Y partially in line (A).) With Y as a friend of X, line (D) of class Y can directly access the private data members m and n of X. By the same token, in line (E) the global print() can also directly access the private members of X.

 
//Friend.cc #include <iostream> using namespace std; class Y; //(A) class X { int m; int n; public: X( int mm, int nn ) { m = mm; n = nn; } friend Y; //(B) friend void print ( X* ); //(C) }; class Y { X* x; int t; public: Y( X* xobj ) { x = xobj; t = x->m + x->n; } //(D) int get_t() { return t; } }; void print( X* ptr ) {cout << ptr->m << " " << ptr->n << endl;} //(E) int main() { X* ptr = new X(100, 200); Y y( ptr ); cout << y.get_t() << endl; // 300 print( ptr ); // 100 200 return 0; }

A friend declaration may be placed in any section of a class, private, protected, or public. It carries the same meaning regardless of where it appears.

Access Control in Java

As mentioned previously, in addition to the modifiers public, private, and protected, Java has one more: package.

The modifiers public and private carry the same meaning in Java as they do in C++. The modifier protected also carries the same meaning, except that such members are like public members in the same package. In other words, a protected class member will be accessible inside all classes in the same package, but to only the subclasses in other package.

When no access modifier is specified in Java, that means the member has access control of type package. Such members are no different from the public members within the same package. But they act like private members with respect to other packages.

What this means is that for a beginning Java programmer, when all of your work is likely to be contained in a single directory, the access modifiers public, package, and protected in your own code behave very much the same. That is, if you leave a member or a method unadorned by not designating any access modifiers, it will exhibit a public-like behavior. The opposite thing to do would be to control its access by using the private modifier.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net