Protection Using Access Specifiers


A programmer controls access to attributes and procedures of a class by using access specifiers within a class definition. An access specifier is a keyword of a programming language that tells the computer what part of the program can access attributes and procedures that are members of a class.

Think of access specifiers as cafeterias. There is the students cafeteria, the teachers cafeteria, and a luncheonette. Each is an access specifier. Only students can use the facilities (attributes and procedures) of the students cafeteria. Only teachers can use the facilities of the teachers cafeteria. However, on occasion, a friend of a teacher is permitted to eat lunch in the teachers cafeteria, and anyone can use the facilities of the luncheonette.

Java and C++ have three kinds of access specifiers ”public, private, and protected. The public access specifier (the luncheonette, in this example) determines attributes and procedures that are accessible by using an instance of the class. The private access specifier (the students and teachers cafeterias) identifies attributes and procedures that are only accessible by a procedure that is a defined by the class. The protected access specifier (a teacher s friend eating at the teachers cafeteria) stipulates attributes and procedures that can be inherited and used by another class. More on inheritance a little later in this chapter.

Public Access Specifier

When you declare an instance of a class (see Chapter 2), you can use the instance to access attributes and procedures that are defined in the public access specifier section of the class. You define the public access specifier section of the class by using the keyword public , as shown in the following example.

This example is written in C++ and requires that a colon follow the keyword public and that public attributes and procedures be defined beneath the public keyword. As you ll see in the Encapsulation in Action Using Java section of this chapter, Java requires that the keyword public be used at the beginning of each definition of public attributes and procedures.

One procedure defined in this example is placed beneath the public access specifier. This means the procedure can be called directly from within the program by declaring an instance of the Student class. Here s the example:

 class Student 
{
public:
void Display(){
//Place statements here
}
};

You can access directly the attributes and procedures defined using the keyword public within the program by using the name of the instance, the dot operator, and the name of the attribute or procedure being accessed.

Let s say that you want to display student information from within a program. Here s what you need to do:

 #include <iostream> 
using namespace std;
class Student
{
public:
void Display(){
cout << "Statements go here." << endl;
}
};
int main() {
Student myStudent;
myStudent.Display();
return 0;
}

The first statement in the main() function declares an instance of the Student class. The second statement calls the Display() procedure of the Student class.

Private Access Specifier

The private access specifier restricts access to attributes and procedures for procedures that are members of the same class. The next example illustrates how this is done. The goal is to prevent the student ID, student name, and graduation status attributes from being used directly by the instance of the Student class. This is accomplished by using the private access specifier.

The private access specifier does not prevent the Display() procedure from accessing these attributes because the Display() procedure is a member of the Student class. Notice that you don t need to create an instance of the Student class to use other members (attributes and procedures) within the procedures of the class.

 #include <iostream> 
using namespace std;
class Student
{
public:
void Display(){
cout << "Student: " << m_ID << " " << m_First << " " <<
m_Last << "
Graduated: " << m_Graduation << endl;
}
private:
int m_ID, m_Graduation;
char m_First[16], m_Last[16];
};
int main() {
Student myStudent;
myStudent.Display();
return 0;
}

The technique shown in the previous example is a cornerstone of object-oriented programming because it requires that other programmers use a member procedure to access attributes of the class. This enables the programmer who created the class to encode rules in member procedures that govern how attributes are to be used.

Suppose you were a programmer who wanted to display student information. You couldn t access student information directly. Instead, you must call a procedure member of the class to display student information. This gives the programmer who defined the class total control over what attributes are accessed and how they are displayed.

Protected Access Specifier

The protected access specifier identifies the attributes and procedures that can be used only by procedures that are members of the class and by procedures that are members of a class that inherits the class.

The class being inherited is called the super class (Java) or the base class (C++), and the class that inherits another class is called the subclass (Java) or the derived class (C++).

Inheritance is covered in Chapter 5, but we ll give you a sneak preview here and throughout this chapter so you ll be able to understand how the protected access specifier works. Let s say that there are two classes. One class is called Student and the other GradStudent .

The Student class contains attributes and behaviors that are characteristic of all students. The GradStudent class contains attributes and behaviors that are unique to graduate students (see Figure 3-2), which include the attributes and behaviors of all students. A graduate student is, after all, a student.

click to expand
Figure 3-2: The GradStudent class has attributes and procedures that are the same and some that are different from the Student class.

Rather than duplicate the attributes and behaviors of the Student class in the GradStudent class, we can use an object-oriented programming language to have the GradStudent class inherit all or some of the attributes and behaviors of the Student class.

Attributes and behaviors defined using the public access specifier and the protected access specifier can be directly used by the GradStudent class. The following C++ example shows how to use the protected access specifier:

 #include <iostream> 
using namespace std;
class Student
{
public:
void Display(){
cout << "Student: " << m_ID << " " << m_First << " "
<< m_Last << "
Graduated: " << m_Graduation << endl;
}
protected:
int m_ID, m_Graduation;
char m_First[16], m_Last[16];
};
int main() {
Student myStudent;
myStudent.Display();
return 0;



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

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