Encapsulation in java. In English
Encapsulation
Java encapsulation is the technique of combining data and code into a single entity, such as a capsule containing a combination of medications. Making each and every one of the class's data members private allows us to construct a fully enclosed Java class. We can now set and retrieve the data in it using the setter and getter methods.
A fundamental concept in object-oriented programming (OOP) is encapsulation, which in Java refers to the grouping of data and methods that manipulate that data into a single unit known as a class. Java encapsulation is a technique for restricting access to a class's implementation details such that only the public interface—which is used to communicate with the class—is visible to outside parties.Data encapsulation is the process of grouping related information into a single unit. It is the connection point between the code and the data it works with. Encapsulation can instead be understood as a protective shield that keeps code outside of it from accessing the data.
- In technical terms, encapsulation means that a class's variables or data are concealed from view by other classes and are only accessible through member functions of the class in which they are stated.
- It is also known as a combination of data-hiding and abstraction. As with encapsulation, the data in a class is hidden from other classes using the data-hiding concept, which is achieved by making the members or methods of a class private. The class is exposed to the end-user or the public without providing any details behind implementation using the abstraction concept.
- Declaring all of the class's variables as private and implementing public methods to set and retrieve variable values are two ways to achieve encapsulation.
- With the setter and getter methods, it is more precisely specified.
- Class Employee {
- private int emp;
- private String email;
- private float salary;
- private long acc;
- public int getEmp() {
- return emp; }
- public String getEmail() {
- return email; }
- public float getSalary() {
- return salary; }
- public long getAcc() {
- return acc; }
- public void setEmp(int emp) {
- this.emp = emp; }
- public void setEmail(String email) {
- this.email = email; }
- public void setSalary(float salary) {
- this.salary = salary; }
- public void setAcc(long acc) {
- this.acc = acc; }}
- class Test {
- public static void main(String[] args) {
- Employee e = new Employee();
- e.setEmp(8120641);
- e.setEmail("nimeshisathya07@gmail.com");
- e.setSalary(200000f);
- e.setAcc(123456789987654321L);
- System.out.println("Employee number:-" + e.getEmp());
- System.out.println("Email:-" + e.getEmail());
- System.out.println("Salary:-" + e.getSalary());
- System.out.println("Account number:-" + e.getAcc());
- }
- }
Output -
Employee number: -8120641
Email: -nimeshisathya07@gmail.com
Salary: - 200000.0
Account number: -123456789987654321
Advantages of Encapsulation: -
- It is a means of limiting our data members' access by concealing the implementation specifics. Additionally, encapsulation offers a means of data hiding. The user won't be aware of how the class is internally implemented. The method by which the class stores values in the variables will not be apparent to the user. The user will only be aware that variables are being initialized with that value and that we are giving the values to a setter method.
- Increased Flexibility
- Additionally, encapsulation increases reusability and is easily adaptable to new requirements.
- Encapsulated code is easy to test for unit testing.
- One of encapsulation's main benefits is that it allows programmers to build a system's details with greater freedom.
- Can result in more complexity, particularly when applied improperly.
- Can make it more challenging to comprehend how the system operates.
- May reduce the implementation's flexibility.
Comments
Post a Comment