Data Encapsulation is an important feature of object oriented programming OOP language. Java Encapsulation means keeping some information (sensitive data) hidden and displaying only relevant information to user. It uses the concept of the concept of binding data and methods in a single unit called class. The phenomenon of combining similar data like variables and methods in a single class helps to achieve encapsulation. It is also known as data hiding.
A class comprises of two members, Data and Methods. By keeping class data private and class methods public encapsulation can be implemented. The steps below help to achieve encapsulation
- Class Data: Define variables and attributes of a class as private
- Class Methods: Create public get and set methods to access and change the values of private variables.
Get Method
Get method is used to accesses and display the value of private variables. The return type of get function can be any of the allowed return types including int, char, double etc. This method returns the private variable so its return type cannot be void. The syntax of writing get method is similar to a regular method. However, the name of get method should start with the word “get”.
Here, variable is the name of private variable defined in the class body. The return statement returns the private variable. By using get method, user can know the data type of private variable. This data type can later be passed as the data type of parameter of set method.
Set Method
Set method assigns a new value to the private variable. It takes the new value as its parameter. The return type of set function can be any of the allowed return types including void, int, char, double etc, depending on the type of private variable. However, the best practice is to define return type as void. The syntax of writing set method is similar to a regular method. However, the name of set method should start with the word “set”.
Here, this is a keyword that is used to refer to the value of original private variable. new_value is the new value assigned to the private variable.
The example below demonstrates the concept of Java Encapsulation:
class Student { private String name = "Peter"; // Get Method public String getName() { return name; } // Set Method public void setName(String newName) { this.name = newName; } } public class Main { public static void main(String[] args) { Student obj = new Student(); System.out.println("The private variable is " + obj.getName()); obj.setName("Alex"); System.out.println("The new private variable is " + obj.getName()); } }
The output of above code after compilation is:
Advantages of Java Encapsulation
- One main advantage of Java Encapsulation is data hiding.
- It maintains the integrity of data as data encapsulation provides the ability to hide sensitive data from the user.
- User can restricts the class attributes to read only or write only format.
- At the same time, it also provides user, the flexibility to read and change the values of private members using getter and setter methods. The best practice is to declare all possible class attributes as private.
- With Java encapsulation, the sensitive data is secure from outside threats and interference as the data is hidden. It also provides an interface to the user to use data members and related functions by creating objects of class.
- In Java Encapsulation, class is the controlling body that has the rights to alter its variables.
Difference between Java Encapsulation and Abstraction
The basic difference between java encapsulation and java abstraction is that encapsulation hides sensitive data while abstraction hides complex implementation details from user. Moreover, Encapsulation uses access modifiers private, public to declare variables and methods respectively. Whereas, abstraction uses abstract methods and classes to override methods.