Encapsulation is defined as the ability to hide data within a class and make it accessible only through methods. A programmer also has the ability to hide methods from a user. There are several reasons why a programmer would want to encapsulate data or methods within a class. Here are a couple of them:
1. A class that has hidden methods can be updated without having to worry about any code a user has written that is dependent on the class.
2. Any methods that are visible should be documented.
3. Any variables that are not hidden can be directly manipulated by a user. Encapsulating all interdependent variables will prevent accidental changes that might stop the program.
Each programming language has its own
unique way of performing encapsulation. Here is a brief look at what
some of the object-oriented languages offer for encapsulation.
Java
Java has four different levels of visibility
or data hiding. The four levels are public, protected, package, and
private. Table 1 shows each of these levels and where the data can
be accessed from for each of them. Default visibility in Java is
package. A default of package helps keep the application programming
interfaces "APIs" cleaner.
|
|
|
|
|
|
| Same class |
|
|
|
|
| Class in same package |
|
|
|
|
| Subclass in different package |
|
|
|
|
| Non subclass, different package |
|
|
|
|
Figure 2 shows how Encapsulation is performed when programming
in Java.
| public class Example {
public void cat ( ) { /* code */ } public void dog ( ) { /* code */ } private void snake ( ) { /* code */ } private void rat ( ) { /* code */ } private float l; // more code } |
C++
There are three different levels of
accessibility in C++. They are public, private, and protected.
Table 3 compares each of their levels of accessibility. If a class
needs to use private members of another class, the key word friend
can be used to give assess to the private members.
|
|
|
|
|
| any function |
|
|
|
| member functions |
|
|
|
| Inherited classes |
|
|
|
There are a lot of rules that apply in C++ when working with encapsulation. Figure 4 gives some code examples for some of these rules.
1. Members of a struct are public by default.
2. Members of a class are private by default.
3. Members of a union are public by default and can not
be changed.
4. Members of a base class that are private can not be
accessed by a class that is inheriting the base class.
| class Example {
int sample; // sample is private by default public:
protected:
struct Example2 {
private:
protected:
union Example3 {
|
Ada 95
|
|
|
|
|
| child package |
|
|
|
| other packages |
|
|
|
By default data is public when writing
programs in Ada 95. If you want to hide this data you must declare
it as private. Figure 5 demonstrates how to encapsulate data in Ada
95.
| package Bank_Account is
type Savings_Account is private; procedure Deposit ( Cash : in out Savings_Account); procedure Statement (Cash : Savings_Account); private
|
Ada has another level of encapsulation.
Any data or subprograms declared in a package body are entirely private
to that package body. A child package has visibility to its parent
package. The public part of the child package has visibility to the
public part of the parent package and the private part of the child package
has visibility to the private part of the parent package. The child
package has no visibility to any members declared in the body of the parent
package.
NOTE:
Ada 95 has the key word protected just like Java
and C++. That is as far as the similarities go. In Java and
C++, protected
is used to encapsulate data or processes. When
protected is used in Ada 95, it is used as a monitor that prevents
more than one task from accessing an object at one time.
Go to the Table of Contents