Understanding encapsulation

After you learn about classes and objects the first and the most important rule which you have to learn and remember while writing your classes is Encapsulation. As per the definition encapsulation means to wrap up of data members and functions inside the class.  This is what the main goal of object oriented programming is. To organize data and functions properly and encapsulation helps you with this organization by putting data members and functions inside the body of the class.

But there is more to encapsulation which you have to understand. Just by saying that if we wrap the data member and functions inside the class we are doing encapsulation then this is wrong. Let me ask you a very simple question first. Do you keep songs in your computer? If yes then how do you keep them? Do you put all the songs in one folder? No. What you do is that you create a folder called Songs. Then inside this folder you create more folders like English, Hindi, Punjabi etc to put different language songs into different folders. Then inside each language folder you will create more folders by the name of the artist. Then inside the artist folder you will create folders by the album name and then finally put the songs inside the album folders. Why do you do this much organization? This is because if you do not organize the songs properly inside different folders then it will become difficult to manage them.

If we just say that putting all the songs in a folder means encapsulation then this will be half correct. Encapsulation does not mean keeping things together. It means keeping connected or similar things together. While traveling you put your cloths in a separate bag and your laptop in a separate bag. This is encapsulation. Let’s write some code to see how we can implement the encapsulation rule in object oriented programming

Math class

Below we have created a math class using pseudo code. In this class we have put functions to do mathematical calculations.

Class Math

// Number1 and Number2 are the data members of the class

Int Number1
Int Number2


// These are the functions of the math class



If you say that encapsulation is to wrap up data and functions inside a class then the code we have written for the Math class is an example of encapsulated code. We have two data members and four functions inside the Math class.

But if you say encapsulation is to keep similar things together then the code for the Math class is not encapsulated. Always remember that we want to create homogeneous classes. We do not want to put everything inside one class and say that we have done encapsulation. In this Math class there are four functions. Add and Subtract functions perform algebraic operations whereas CalculateArea and CalculateVolume functions perform geometric operations. Algebra and Geometry are two different concepts in mathematics. Therefore we should keep the functions performing algebric operation and geometric operations in separate classes.

Look at the code below

Class Algebra

Int Number1;
Int Number2;



Class Geometry

Int Number1;
Int Number2;



We have now divided the Math class into two classes Algebra and Geometry and have put functions related to Algebra into the Algebra class and functions related to Geometry into the Geometry class. Now we can say that we have followed proper encapsulation because we have grouped similar things together.

Encapsulation is used everywhere

Please keep in mind that the concept of encapsulation is not just used in object oriented programming, you use it in everything which you do. When you are setting up your cupboard in your room you put the cloths and books on separate shelves. This is encapsulation. In your house you keep things related to cooking in your kitchen and things related to your studies in your study room. This is encapsulation. Similarly there is encapsulation in everything you do in your daily lives. All you need to do is start understanding the real meaning of encapsulation and you will start appreciating this concept a lot.

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp