Encapsulation

    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.
 
Table 1 Accessibility in Java
Accessible to:
public
protected
package
private
Same class
yes
yes
yes
yes
Class in same package
yes
yes
yes
no
Subclass in different package
yes
yes
no
no
Non subclass, different package
yes
no
no
no

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
}
Figure 2  Java encapsulation example

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.
 

Table 3 Accessibility in C++
Accessible to:
public
private
protected
any function
yes
no
no
member functions
yes
yes
yes
Inherited classes
yes
no
yes

    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:
     int users;  // set to public access

protected:
     char names; // set to protected access
};

struct Example2 {
     int sample; // sample is public by default

private:
     int users; // set to private access

protected:
     char names; // set to protected access
};

union Example3 {
     int sample; // public by default no other choices
}; 

Figure 4  C++ encapsulation example

 

Ada 95
 
 

Accessible to:
public
private
limited private
child package
yes
yes
yes
other packages
yes
**
no
Table 5  Accessibility in Ada 95 
** Can only be passed by a client of a package as a parameter to a function, used as a return type, assigned to (:=), checked for equality (=), or checked for inequality (/=): no other operations are supported unless explicitly declared.  This is because the client cannot see the structure of the full declaration.

    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
  type Savings_Account is
      record
          ...
      end record;
end Bank_Account;

Figure 5  Ada 95 encapsulation example

    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