The process of binding data and corresponding methods (behavior) together into a single unit is called encapsulation in Java.

In other words, encapsulation is a programming technique that binds the class members (variables and methods) together and prevents them from being accessed by other classes.

Thereby, we can keep variables and methods safes from outside interference and misuse.

Every Java class is an example of encapsulation because we write everything within the class only that binds variables and methods together and hides their complexity from other classes.

Another example of encapsulation is a capsule. Basically, the capsule encapsulates several combinations of medicine.

If combinations of medicine are variables and methods then the capsule will act as a class and the whole process is called Encapsulation as shown in the below figure.

In the encapsulation technique, we declare fields as private in the class to prevent other classes from accessing them directly. The required encapsulated data can be accessed by using the public Java getter and setter method.

 If the field is declared private in the class, then it cannot be accessed by anyone from outside the class and hides the field within the class. Therefore, it is also called data hiding.

Let’s understand Encapsulation in java better by taking real-time examples.


Realtime Example of Encapsulation in Java

Example 1:

Schoolbag is one of the most real examples of Encapsulation. Schoolbags can keep our books, pens, etc.


Example 2:

When you log into your email accounts such as Gmail, Yahoo Mail, or Rediff mail, there is a lot of internal processes taking place in the backend and you have no control over it.

 When you enter the password for logging, they are retrieved in an encrypted form and verified, and then you are given access to your account.

 You do not have control over it that how the password has been verified. Thus, it keeps our account safe from being misused.

Example 3:

Suppose you have an account in the bank. If your balance variable is declared as a public variable in the bank software, your account balance will be known as public, in this case, anyone can know your account balance. So, would you like it? Obviously, No.

So, they declare the balance variable as private for making your account safe, so that anyone cannot see your account balance.

The person who has to see his account balance, will have to access only private members through methods defined inside that class and this method will ask your account holder name or user Id, and password for authentication.

Thus, we can achieve security by utilizing the concept of data hiding. This is called Encapsulation in Java.


How to achieve or implement Encapsulation in Java

There are two important points whereby we can achieve or implement encapsulation in the Java program.

1. Declaring the instance variable of the class as private. so that it cannot be accessed directly by anyone from outside the class.

2. Provide the public setter and getter methods in the class to set/modify the values of the variable/fields.



There are the following advantages of encapsulation in Java. They are as follows:

1. The encapsulated code is more flexible and easier to change with new requirements.

2. It prevents the other classes to access the private fields.

3. Encapsulation allows modifying implemented code without breaking other code that has implemented the code.

4. It keeps the data and codes safe from external inheritance. Thus, Encapsulation helps to achieve security.

5. It improves the maintainability of the application.

6. If you don’t define the setter method in the class then the fields can be made read-only.

7. If you don’t define the getter method in the class then the fields can be made write-only.


A disadvantage of Encapsulation in Java

The main disadvantage of encapsulation in Java is it increases the length of the code and slows shutdown execution.

Post a Comment