Encapsulation in Java
Overview
Data Encapsulation, a fundamental concept of Object-Oriented Programming, binds or bundles the related code units together, making the code more organized. Encapsulation helps to hide data by preventing unauthorized access to the implementation details.
What is Encapsulation in Java?
Data Encapsulation can be defined as wrapping the code or methods(properties) and the related fields or variables together as a single unit. In object-oriented programming, we call this single unit - a class, interface, etc. We can visualize it like a medical capsule (as the name suggests, too), wherein the enclosed medicine can be compared to fields and methods of a class.
The variables or fields can be accessed by methods of their class and can be hidden from the other classes using private access modifiers. One thing to note here is that data hiding and encapsulation may sound the same but different.
Example of Data Encapsulation
Below is a program to calculate the perimeter of a rectangle
Output:
Data Hiding
Access modifiers are used to achieve data hiding. Access modifiers are the keywords that specify the accessibility or the scope of methods, classes, fields, and other members.
Difference between encapsulation and data hiding is that Encapsulation is a way of bundling data whereas Data Hiding prevents external unauthorized access to the sensitive data.
The four types of access modifiers in Java are:
- Public: A public modifier is accessible to everyone. It can be accessed from within the class, outside the class, within the package, and outside the package.
- Private: A private modifier can not be accessed outside the class. It provides restricted access. Example:
- Protected: A protected modifier can be accessed from the classes of the same package and outside the package only through inheritance.
- Default: A default modifier is accessible only within the package. If no modifier is used, it is treated as default.
Access Modifier | Within Class | Within Package | Same Package by Subclasses | Outside Package by Subclasses | Global |
---|---|---|---|---|---|
Public | Yes | Yes | Yes | Yes | Yes |
Protected | Yes | Yes | Yes | Yes | No |
Default | Yes | Yes | Yes | No | No |
Private | Yes | No | No | No | No |
A Private Access Modifier is used for the purpose of Data Hiding.
Example of Data Hiding:
Output:
In the next section, we'll see how some methods are defined to tackle this error. Use this Online Compiler to compile your code.
Getter and Setter Methods
As we can't directly read and set the values of private variables/fields, Java provides us with getter and setter methods to do so.
To find out more about these methods, refer Getter And Setter Methods.
How to implement Encapsulation in Java?
We need to perform two steps to achieve the purpose of Encapsulation in Java.
- Use the private access modifier to declare all variables/fields of class as private.
- Define public getter and setter methods to read and modify/set the values of the abovesaid fields.
Example:
Benefits of Encapsulation java
- Cleaner, more organized and less complex code.
- More flexible code as can modify a unit independently without changing any other unit.
- Makes the code more secure.
- The code can be maintained at any point without breaking the classes that use the code.
Key Points to Remember about encapsulation in Java
There are two golden rules to remember about encapsulation in Java:
- Declare all the variables as private
- Write public getter and setter methods to access these variables
Also, data hiding and data encapsulation are interchangeably used in most cases but are different.
FAQs
Q: Why Is Encapsulation used in Java?
A: Encapsulation is used to bind the fields and methods together. It mainly aims at bundling and hiding the data and controlling access to it.
Q: What is Encapsulation in OOP?
A: Encapsulation deals with binding the data together as one unit and protecting it from unauthorized access.
Q: What is the difference between abstraction and encapsulation?
A: Abstraction is a technique for hiding implementation details. Encapsulation, on the other hand, is a way of hiding data in a single entity or unit and protecting information from the outside.
Conclusion
- Data Encapsulation is the mechanism of wrapping up methods/functions and the data/fields it manipulates together as a single unit.
- For implementing data encapsulation in Java, private access modifier, getter, and setter methods are used.
- Data Hiding and Encapsulation are often interchangeably used, but they have different meanings.