Encapsulation is an act of combining properties and methods of the same entity. In other words, it is a process of wrapping the data (variables) and code together in a single unit. It is an OOPs concept. Encapsulation is also known as data hiding because the data in a class is hidden from other classes. A capsule is an example of encapsulation which is mixed of several medicines.
This Java Tutorial will explain all about Encapsulation and Data Hiding with Examples. In fact, it covers entire information on how to achieve Encapsulation in Java, Advantages, Encapsulation in Java with Example, Abstraction Vs Encapsulation.
- What is Encapsulation?
- How to Achieve Encapsulation in Java?
- Advantages of Encapsulation
- Encapsulation in Java Example
- Read-Only Class
- Write-Only Class
- Abstraction Vs Encapsulation in Java OOPS
What is Encapsulation?
The Complete Idea behind implementing Encapsulation is to hide the implementation data from users. If at all a Data Member is Private then it can only be accessed within the class and no outside class can use this private data member of the other class.
How to Achieve Encapsulation in Java?
In Java, we can create a fully encapsulated class by declaring all the data members (variables) of the class private. If the data member is private then it can be only accessible within the same class. No other class can access the data member of that class.
Now the question arises if we are declaring all the data members (variables) of the class private then how should we use this data member. So the answer is we can use the setter and getter method to set and get the value of data member (variable) of the class private. We can implement encapsulation in java by using the JavaBean class. The following code is the example of a getter and setter method.
package com.javastudypoint; class Employee{ private int id; private String name; public void setName(String name){ this.name = name; } public String getName(){ return name; } }
Advantages of Encapsulation
Data Hiding: Encapsulation is a way to achieve the data hiding in java so that the other class will not be able to access the private members of the class. In Encapsulation, we can hide the internal information of the data which is better for security concerns.
Increased Flexibility: With Java Encapsulation, we can make the class read-only i.e. A class that has only getter methods, and write-only i.e. A class that has only setter methods. If we don’t want to change the value of a variable, we can use the read-only class. If we want to change the value of a variable, we can use the write-only class.
In Encapsulation, we can combine the variables and methods in a single unit. So that the encapsulation provides control over the data.
Reusability: Encapsulation improves the reusability and makes it easy to change as per new requirements.
Testing Code is Easy: Encapsulated Code is quite simple to test when it comes to Unit Testing.
See More:
Encapsulation in Java Example
In this example, we are going to show you how to achieve Encapsulation in Java. To achieve Encapsulation in Java we need private member and getter and setter method. setter and getter method set and get the value of data member (variable).
class Employee{ private int empId; private String empName; private int empAge; public void setempId(int empId){ this.empId = empId; } public int getempId(){ return empId; } public void setempName(String empName){ this.empName = empName; } public String getempName(){ return empName; } public void setempAge(int empAge){ this.empAge = empAge; } public int getempAge(){ return empAge; } } class EncapsulateExample{ public static void main(String args[]){ Employee emp = new Employee(); emp.setempId(101); emp.setempName("Prashant"); emp.setempAge(21); System.out.println("Employee Id is: " +emp.getempId()); System.out.println("Employee name is: " +emp.getempName()); System.out.println("Employee age is: " +emp.getempAge()); } }
Output:
Read-Only Class
//A Java class which has only getter methods. public class Student{ //private data member private String college="AKG"; //getter method for college public String getCollege(){ return college; } }
We can’t change the value of the college data member that is “AKG”.
s.setCollege("KITE");//will render compile time error
Write-Only Class
//A Java class which has only setter methods. public class Student{ //private data member private String college; //getter method for college public void setCollege(String college){ this.college=college; } }
We can’t change the value of the college but we can change the value of the college data member.
System.out.println(s.getCollege());//Compile Time Error, as there is no such method System.out.println(s.college);//Compile Time Error, since the college data member is private. //So, it can't be accessed from outside the class
Abstraction Vs Encapsulation in Java OOPS
Usually, most people misunderstand encapsulation with Abstraction. However, it is different from it, and let’s get into details on how it differs.
- Encapsulation is all about How to Achieve a Functionality
- Abstraction is more about What a Class Can do.
A Simple Example to understand the difference is a Mobile Phone. Complex Logic in the circuit board is encapsulated in a Touch Screen and an Interface is provided to abstract it.