Encapsulation in Java | Data Encapsulation in Java
In this article Encapsulation in Java is used for wrapping data (variables and methods) under a single unit. Encapsulation means ensuring that “sensitive” data is hidden from users.
Encapsulation in Java:
In encapsulation, a class variables will be hidden from other classes, and can only be accessed through the methods of their current class.
Java Encapsulation is used for wrapping data (variables and methods) under a single unit. Encapsulation is a protective shield that prevents data from being accessed by code outside this shield. Encapsulation means ensuring that “sensitive” data is hidden from users.
To achieve encapsulation in Java we have to:
• The variables of a class have to be declared private.
• Public setter and getter methods must be provided to access and modify the value of the variable.
In encapsulation, a class’s variables will be hidden from other classes, and can only be accessed through the methods of their current class. Hence, it is also known as data hiding.
Encapsulation is one of the four fundamental OOP concepts. The other three are Inheritance, Polymorphism and Abstraction.
It is understood in this way, as there are many components in a gastritis capsule. For a user who does not need to know, only one capsule is enough for a user.
Getter and Setter Methods in Java:-
From the previous chapter we saw that private variables can be accessed only within the same class. However, if we provide public get and set methods, access to private variables is also possible.
public class Student
{
// Private data member
private String name;
private int roll;
// Public Getter and setter methods
public String getName()
{
return name;
}
public void setName(String newName)
{
this.name = newName;
}
public int getRoll()
{
return roll;
}
public void setRoll(int newRoll)
{
this.roll = newRoll;
}
}
Benefits of Java Encapsulation
• Fields of a class can be made to be read-only or write-only.
• Flexible: The programmer can change a part of the code without affecting the other parts.
• Data Hiding: Users will not have any idea about the inner implementation of the class.
How to achieve encapsulation
Step 1. By declaring all the variables of the class as private
Step 2. Define public getter and setter methods to access or modify the value of the variable
Encapsulation Vs Data Hiding
Often, people think of Encapsulation and Data Hiding as the same, and often use them interchangeably, but this is not the case at all.
The task of encapsulation is to keep the data and its related methods together by making a group/group and keeping it as a unit, which provides better management and security.
For example, you can relate it to your society like, there are many rooms in your society, swimming pool, gym, guest-house, audit room, etc.
Now who has put all these together, your own society, so in this way your society is playing the role of Encapsulation here.
Data hiding, on the other hand, restricts access to the data member, hiding its implementation details.
Encapsulation bundles the data together and keeps it as a single unit, in this way it also hides the data. It also makes the data private, so that the data cannot be accessed outside class.
In addition to hiding the data, it provides only relevant (only what is needed) details to the end-user, without exposing the implementation details, which is called abstraction. In this way we can say that, encapsulation is a combination of both abstraction and data-hiding.
Advantage of Encapsulation in Java
1. Through getter and setter method we can make any class read-only or write-only.
2. Through this we can control the data, for example if a user types 5 digit (digit) while typing ATM PIN, then we can give a message to that user that, type only 4 digit.
3. It helps in achieving data hiding.
4. It protects the data from unauthorized access i.e. provides security.
5. Since it keeps data and methods grouped together, it becomes easy to manage and maintain the code.
6. It helps in code enhancement, in future if we want to change the implementation part of any method, we can do it easily, without modifying any other method.
Disadvantage of Encapsulation in Java
1. The length of the code increases because the validation code has to be implemented everywhere, such as implementing PIN, id, password for authentication.
2. Performance becomes slow because, execution takes more time.
Related Link:
- Object Oriented Programming Using C++ |BCA Semester II |History of C++
- Core Java Programming | BCA Part III | SEM-VI | What is Java
- Computer Fundamentals Notes | Computer Fundamentals Tutorial
- DOT NET Technology Tutorial | ASP.NET Tutorial for Beginners