Java Encapsulation
# Java Encapsulation
* * *
In object-oriented programming methodology, encapsulation is defined as a methodology of bundling the data and the functions that work on that data within a single unit.
Encapsulation can be thought of as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the code and data is tightly controlled by an interface.
The primary benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. This gives us a decoupled nature.
Encapsulation also makes the code easier to understand and maintain, and enhances security.
### Advantages of Encapsulation
* 1. Good encapsulation reduces coupling.
* 2. The internal structure of the class can be freely modified.
* 3. More precise control can be exercised over member variables.
* 4. Hides information and implementation details.
* * *
## Steps to Implement Java Encapsulation
1. Change the visibility of the class's variables to private (typically). For example:
public class Person{private String name; private int age; }
In this code, the **name** and **age** attributes are set to private, meaning they can only be accessed within the class itself. Other classes cannot access them directly, thus hiding the information.
2. Provide public getter and setter methods to access and update the value of a private variable. For example:
public class Person{private String name; private int age; β public int getAge(){return age; } β public String getName(){return name; } β public void setAge(int age){this.age = age; } β public void setName(String name){this.name = name; }}
The **this** keyword is used to resolve the name conflict between the instance variable (private String name) and the local variable (the name variable in setName(String name)).
* * *
## Example
Let's look at an example of a Java encapsulation class:
## EncapTest.java File Code:
/* File name: EncapTest.java */public class EncapTest{private String name; private String idNum; private int age; public int getAge(){return age; }public String getName(){return name; }public String getIdNum(){return idNum; }public void setAge(int newAge){age = newAge; }public void setName(String newName){name = newName; }public void setIdNum(String newId){idNum = newId; }}
In the above example, the public methods are the entry points for external classes to access the member variables of this class.
Typically, these methods are called getter and setter methods.
Therefore, any class that wants to access the private member variables of a class must do so through these getter and setter methods.
The following example demonstrates how the variables of the EncapTest class are accessed:
## RunEncap.java File Code:
/* File name: RunEncap.java */public class RunEncap{public static void main(String args[]){EncapTest encap = new EncapTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName()+ " Age : "+ encap.getAge());}}
The compiled and running result of the above code is as follows:
Name : James Age : 20
YouTip