Encapsulation
is the process of combining data and functions into a single unit called class.
Using the method of encapsulation, the programmer cannot directly access the
data. Data is only accessible through the functions present inside the class.
Data encapsulation led to the important concept of data hiding. Data hiding is
the implementation details of a class that are hidden from the user. The
concept of restricted access led programmers to write specialized functions or
methods for performing the operations on hidden members of the class. Attention
must be paid to ensure that the class is designed properly.
Neither too much
access nor too much control must be placed on the operations in order to make
the class user friendly. Hiding the implementation details and providing
restrictive access leads to the concept of abstract data type. Encapsulation
leads to the concept of data hiding, but the concept of encapsulation must not
be restricted to information hiding. Encapsulation clearly represents the
ability to bundle related data and functionality within a single, autonomous
entity called a class.
For instance:
class
Exforsys
{ public: int sample(); int example(char *se) int endfunc(); ......... ......... //Other member functions private: int x;float sq; .......... ......... //Other data members }; |
In the
above example, the data members integer x, float sq and other data members and
member functions sample(),example(char* se),endfunc() and other member
functions are bundled and put inside a single autonomous entity called class
Exforsys. This exemplifies the concept of Encapsulation. This special feature
is available in object-oriented language C++ but not available in procedural
language C. There are advantages of using this encapsulated approach in C++.
One advantage is that it reduces human errors. The data and functions bundled
inside the class take total control of maintenance and thus human errors are
reduced. It is clear from the above example that the encapsulated objects act
as a black box for other parts of the program through interaction. Although
encapsulated objects provide functionality, the calling objects will not know
the implementation details. This enhances the security of the application.
The key strength behind Data Encapsulation in C++ is that the keywords or the access specifiers can be placed in the class declaration as public, protected or private. A class placed after the keyword public is accessible to all the users of the class. The elements placed after the keyword private are accessible only to the methods of the class. In between the public and the private access specifiers, there exists the protected access specifier. Elements placed after the keyword protected are accessible only to the methods of the class or classes derived from that class.
The concept of
encapsulation shows that a non-member function cannot access an object's
private or protected data. This adds security, but in some cases the programmer
might require an unrelated function to operate on an object of two different
classes. The programmer is then able to utilize the concept of friend
functions. Encapsulation alone is a powerful feature that leads to information
hiding, abstract data type and friend functions.
Features and Advantages of the concept of Encapsulation:
1. Makes
Maintenance of Application Easier:
Complex
and critical applications are difficult to maintain. The cost associated with
maintaining the application is higher than that of developing the application
properly. To resolve this maintenance difficulty, the object-oriented
programming language C++ created the concept of encapsulation which bundles
data and related functions together as a unit called class. Thus, making
maintenance much easier on the class level.
2. Enhanced Security:
There
are numerous reasons for the enhancement of security using the concept of
Encapsulation in C++. The access specifier acts as the key strength behind the
concept of security and provides access to members of class as needed by users.
This prevents unauthorized access. If an application needs to be extended or
customized in later stages of development, the task of adding new functions
becomes easier without breaking existing code or applications, there by giving
an additional security to existing application.
3.Improves
the Understandability of the Application
No comments:
Write comments