Starting with Encapsulation

Arun K. Singh
OOP Concepts with ciro.do by Arun K.
3 min readAug 2, 2020

The first module that I attempted is of Encapsulation. All the things I was going to do was provided at Crio’s Learning Platform. Here every module was organized in a structured manner. Every module was having some specific tasks related to the corresponding concept and after completion of all tasks, I was all set with that concept. I felt that completing all task makes me understand the concept so well that I am now able to apply the concept of Encapsulation in real life actual projects of mine.

Encapsulation in formal definition means grouping all related functionality in one place. Apart from being cohesive (related fields and methods being together), it helps in securing the data fields of an object from being altered directly by external methods or objects. Encapsulation safeguards the internal contents of the class like real-life capsule. Let me explain with a simple example of what encapsulation is. Consider the example of Human Body. We can walk, speak, move, hear etc. with the organs like legs, mouth, hand, ears etc. Now here all functionality and related fields are encapsulated together into a single entity called a human body. That is how we achieve Encapsulation.

Encapsulation in OOPs is achieved using classes. All the Variables and Methods are defined under the same class and the objects of that class are created for the use. Based on the type of access specifiers used for the class i.e private or public the methods and Variables are accessible via class objects. The code which was provided to us in this module was implemented by both OOP and Non-OOP ways. I first refer to the Non OOP way implementation and tries to identify the flaws in the design. Then I referred the OOP way implementation and I found what was actually a flaw in the previous one.

Encapsulation ensures that only the necessary sections of the object are modifiable across all the methods which was not in the case with the previous code that I saw. We generally make variables of the class private to achieve the encapsulation so that external objects can not directly access them. They can only access them through class methods. This provides security to the data. Encapsulation provides Reusability, Maintainability and Data Hiding.

Apart from this, I learned a lot about encapsulation which was specific to languages like how we can achieve encapsulation in Java,C++ and Python. The takeaway was very useful. Now I know how to apply Encapsulation in my code. Next, I will share my Journey to Abstraction and what is the subtle difference between Abstraction and Encapsulation. So follow my next story to visit the House of Abstraction.

--

--