Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     

Encapsulation and Data hiding

Encapsulation means to put different things in a capsule i.e. single unit. In case of OOPs, encapsulation means to bind together or wrap up data and functions that operate on data. These functions provide the interface through which the outer world can access the data.

The concept of encapsulation is implemented using class in C++. The data and functions are put together in the class.

Data hiding is closely related with encapsulation. Encapsulation is must to achieve data hiding. In encapsulation we put the data and functions in one area i.e. class. The data is accessible only through functions and not directly. Thus it is hidden from outside world. To make it hidden it is declared as private inside the class and only functions of the class can access it.

Thus with encapsulation we draw a boundary around data and the data is accessible only by the functions that lie within that boundary and not those that are outside it.

Here student class has three data members rollno, name and marks, and two member functions input ( ) and show ( ). The data is hidden from outside world and is only accessible by two functions inside class.

Main advantage of encapsulation and data hiding is that they protect the data from outside world so that the data should not be misused or manipulated.

Share this article   |    Print    |    Article read by 4561 times
Rohit kakria
I am software developer, moderator of
Related Articles: No related article
Related Interview Questions: No related interview question