Encapsulation is one of the four OOP concepts. The other three are Inheritance, Polymorphism, and Abstraction.

In Java, encapsulation is a mechanism of wrapping data (variables) and code together as a single unit.

To understand what is encapsulation? We will see an example of a banking application created in our previous tutorial.

Please be patient. The Video will load in some time. If you still face issue viewing video click here

Suppose a hacker managed to gain access to the code of your bank account. Now, he tries to deposit amount -100 into your account by two ways. Let see his first method or approach.

Approach 1: He tries to deposit an invalid amount (say -100) into your bank account by manipulating the code.

Learn Java Encapsulation in 10 Minutes

Now, the question is – Is that possible?

Let see whether it is possible or not? Usually, a variable and class is set as a "Private" as shown below. It can only be accessed with the corresponding methods, and no other alternatives methods are allowed to carry out the operations.

Learn Java Encapsulation in 10 Minutes

If a data member is private, it means it can only be accessed within the same class. No outside class can access private data member or variable of other class.

So in our case hacker cannot deposit amount -100 to your account.

Learn Java Encapsulation in 10 Minutes

Approach 2: Hacker's first approach failed to deposit the amount. Next, he tries to do same by using "deposit" method. To carry out this operation he uses "deposit" method.

Learn Java Encapsulation in 10 Minutes

But method implementation has check for negative values. So the second approach also fails in depositing amount -100 into your account.

Learn Java Encapsulation in 10 Minutes

Thus, you never expose your data to an external party. Which makes your application secure.

Learn Java Encapsulation in 10 Minutes

The entire code can be thought of a capsule, and you can only communicate through the messages. Hence the name encapsulation.

Few notes on Encapsulation in Java :

  • In Java, encapsulation is binding the data with its related functionalities
  • Here functionalities mean "methods" and data means "variables"
  • So we keep variable and methods in one place. That place is "class."
  • "Class" is the base for encapsulation.
  • With Java Encapsulation, you can hide (restrict access) to critical data members in your code, which improves security
  • As we discussed earlier, if a data member is declared "private", then it can only be accessed within the same class. No outside class can access data member (variable) of other class.
  • However if you need to access these variables, you have to use public "getter" and "setter" methods.
  • Setup public "getter" and "setter" method to update and read the private data field. This will allow data access from private class.

Frequently, java encapsulation is referred for data hiding. But more than data hiding, encapsulation concept is meant for better management or grouping of related data.

To achieve a lesser degree of encapsulation in Java you can use modifiers like "protected" or "public". With encapsulation, developers can change one part of the code easily without affecting other.

Often encapsulation is misunderstood with Abstraction. To get clear on this,

  • Encapsulation is more about "How" to achieve that functionality
  • Abstraction is more about "what" a class can do.

A simple example to understand this difference is a mobile phone. Where the complex logic in the circuit board is encapsulated in a touch screen, and the interface is provided to abstract it out.

 

YOU MIGHT LIKE: