Introduction

One key concept that every programmer should understand is encapsulation. Encapsulation is a powerful object-oriented programming technique that allows developers to protect and manage the access to data within their code. This tutorial will delve into the meaning of encapsulation, its importance in programming, and will provide practical examples to help you understand how you can use it in your own projects.

Table of Contents :

  • What is Encapsulation
  • What is data hiding
  • Encapsulation Vs Data Hiding 
  • Real world example of Encapsulation and data hiding 

What is Encapsulation

  • The process of bundling the data and the code that works on that data together is called encapsulation.
  • in any programming language when we define a class, we are in practice implementing encapsulation.
  • We are encapsulating the data members and data functions of a class as one unit.
  • Encapsulation decreases and hides the complexity of our code.
  • Encapsulation ensures that the end user can focus on ‘what to do’ with the system rather than ‘how it is done’.
  • We can understand encapsulation with a real world example of applying brakes of a car.
  • When we apply the brakes of a car - 
    • We are only concerned about what lever we have to push to apply the brakes.
    • We need not worry about the internal mechanisms that take place after we push the lever.
    • We are unaware of what wires are being pulled through the lever,  what is the function of brake shoes etc.
  • Encapsulation is a sub-process of data hiding.

What is data hiding

  • In object oriented programming data hiding is a very important concept
  • Data hiding is more related to the security of the system.
  • It ensures that the members of a class are protected from any illegal access by any outside code.
  • Data hiding restricts the access of class members from any other code outside the class.
  • i.e. the data members of a class can be accessed through member functions of that class only.
  • In other words, Data hiding ensures that both data and the code is safe from outside code.
  • The main difference between encapsulation and data hiding is that 
    • encapsulation is more concerned on reducing and hiding the complexity of the system whereas 
    • data hiding is more concerned on the security of the system.
  • An example of data hiding is the information of our social media accounts - 
    • The non-sensitive information of our social media accounts is visible to outside world.
    • But the sensitive information like passwords, security question etc. is invisible to it.
    • The outside world is not allowed to manipulate even our publicly visible information.
  • Data hiding can be achieved by making use of encapsulation along with access modifiers.

Encapsulation Vs Data Hiding 

The main differences between encapsulation and data hiding are listed below in a tabular form :

Data HidingEncapsulation
Data hiding is more concerned on system securityEncapsulation focuses more on hiding the complexity of the system.
Data hiding secures the data from unauthorized code.Encapsulation bundles the data and functions together as a single unit.
Data hiding is a process and a technique. It can be implemented by using encapsulation and access modifiers.Encapsulation is a sub-process of Data hiding.
Depending upon requirement, the data can be made private or protected thus making data inaccessible from outside code. In encapsulation data can be public as well.

Real world example of Encapsulation and data hiding :

Consider a college with students in different streams. There is a different department for each stream. Each department maintain the attendance details of all the students in its stream.

  • Suppose someday for some reason department A needs the attendance details of a particular student of department B.
  • Now department A cannot access the records of department B directly.
  • There has to be a point of contact in department B who is authorized to access the records of the students.
  • This person will search for the required record and hand it over to department B.
  • Also the record of only this particular student will be shown to department B.
  • The records of the rest of the students will remain hidden.

Prev. Tutorial : What are Objects

Next Tutorial : What is Data Abstraction