Chapter 3: Encapsulation


Encapsulation is one of those computer terms that has an overtone of sci-fi and rocket science combined, and yet you use encapsulation every day of your life. Do you have your credit cards, money, and your driver s licenses in your wallet? Do you have pens, paper, textbook , and your laptop in a bag that you take to school? If so, then you use encapsulation. Encapsulation is putting related things together to form a new object. It may seem trivial, but you ll learn in this chapter that encapsulation has revolutionized the way programs are written and has become a cornerstone of object-oriented programming.

Parts of a Program Come Together

For a long time, programmers used procedural programming languages such as C to group together instructions into one task, called a procedure . A procedure is the same as a function in C and C++ and a method in Java.

Think of a procedure as the definition of an object s behavior. For example, there is a procedure for a student to register for a course. The student is the object and the procedure, to register for a course, is a behavior performed by a student. Attributes of a student, such as student ID, are used to carry out the procedure.

In the real world, objects and their behaviors and attributes are grouped together. For example, you can t register for a course if you are not a student. Steps are taken to prevent someone who is not a student from receiving a student ID and from submitting a course registration. This is because attributes of a student and behaviors of a student are grouped together and associated with a student. If you are not a student, then you cannot perform the behaviors of a student.

However, in the world of procedural programming, procedures and attributes are not grouped together and associated with an object. This means a programmer could call the registration procedure to register a person who is not a student.

The following example illustrates this problem. This is a C/C++ program that defines a function (procedure) called registration . The registration() function receives a student ID and course number in its argument list and displays those values in a message on the screen. As you ll recall from your programming classes, an argument list contains information needed for the procedure to carry out its task.

Two variables (attributes) are declared in the main() function. These are studentID and courseNumber . Each is initialized with a value, which is passed to the registration() function in the next statement.

Notice that there is no association between variables and the registration() method, except that variable names and the name of the function imply they have something to do with a student:

 #include <string> 
#include <iostream>
using namespace std;
void registration(string studentID, string courseNumber)
{
cout << "Registration Accepted: " + studentID + " "
+ courseNumber << endl;
}
int main()
{
string studentID = "12345", courseNumber = "9876";
registration(studentID, courseNumber);
return 0;
}

The lack of an association between attributes and a procedure is a drawback of procedural programming languages. This is of little concern when one programmer develops an entire application because the programmer knows not to pass the registration() function a variable containing a nonstudent. It becomes troublesome when a team of programmers designs the application because each programmer on the team must remember to pass the registration() function only student information.

Here s the problem: There is no way in a procedural programming language to force an association between attributes and procedures, which can lead to inadvertent misuse of the association by the programming team.

Here s the solution: Write the program using an object-oriented programming language such as C++ or Java. An object-oriented programming language enables a programmer to encapsulate attributes and procedures and associate them with an object. This greatly reduces the misuse to attributes and procedures.

The solution came with the introduction of object-oriented programming in the 1980s. Object-oriented programming uses an object-oriented programming language such as C++ or Java to mimic real-world objects in a program by defining a class.




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