- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Advantages of encapsulation in Java
Encapsulation in Java is defined as the process of binding data and its corresponding methods (behavior) together under a single unit.
To put it in simple words, encapsulation is nothing but a programming technique used to wrap the class members (variables and methods) together.
After implementing encapsulation, the variables and the data of one class cannot be accessed in another class whereas that data can be used by the member functions of the same class. This in turn helps in protecting variables and methods from outside interference and misuse.
We understand Encapsulation in Java may look very intimidating for you as a concept at first. But, don’t worry, reading this article, you will learn what is encapsulation with a real-life example first. As we move further, you will also learn why we need encapsulation in Java, the ways to achieve Encapsulation in Java, and the advantages of Encapsulation in Java.
Let’s get started!
Encapsulation in Java With Real-time Example
From the above discussion, we hope you understand that Encapsulation refers to the act of encapsulating or wrapping up data.
For your better understanding of encapsulation in Java, let’s take some real-time examples!
Example 1 − Imagine you are traveling to a place to attend an important exam. Let’s say UPSC, SSC, or anything of your choice. But whenever an exam comes to mind there is one specific thing we all do. Preparation and Arrangement. We carefully arrange all the exam needs such as books, stationery items, hall tickets, and clothes inside a bag or a trolley. This concept of binding up necessary things to prevent other people from using them is Encapsulation. Here your bag or Trolley is one of the most real examples of Encapsulation.
Example 2 − Another example of encapsulation is a capsule. Generally, the capsule encapsulates various combinations of medicine.
If the combinations of medicine are taken as variables and methods, then the capsule will be the class and the whole process is known as Encapsulation.
Why Do We Need Encapsulation in Java?
Encapsulation provides tons of benefits in everyday programming. Some of the exhilarating benefits are as follows −
Programming is made flexible with encapsulation. It means you do not need to edit and update code every time according to new specifications.
It helps you in achieving loose coupling.
With encapsulation, it's simple and easy to debug an application.
It is also possible to alter and make edits to your codebase without disrupting the regular functioning of your program.
It enables the programmer to monitor the data accessibility of a class.
Ways to Achieve Encapsulation in Java
There are two important ways through which you can achieve or implement encapsulation in the Java programming language.
By setting the instance variable of the class as private, so that it cannot be used directly by anyone from outside the class.
Java program explaining the implementation of encapsulation by using private variables −
public class TestEncapsulation { private String privateVar; TestEncapsulation() { privateVar = "java"; } } public class PrivateVariables { public static void main(String[] args) { System.out.println(TestEncapsulation.privateVar); } }
Output
TestEncapsulation.java:7: error: class PrivateVariables is public, should be declared in a file named PrivateVariables.java public class PrivateVariables { ^ TestEncapsulation.java:9: error: privateVar has private access in TestEncapsulation System.out.println(TestEncapsulation.privateVar); ^ 2 errors
From the above example of the private access specifier, you can notice the class cannot use the variable from the previous class.
Set and get the values of variable/fields in the class using public setter and getter methods.
Java program to illustrate the use of getter and setter methods in Java program
package com.dataflair.encapsulation; class TestEncapsulation { private String privateVar; TestEncapsulation() { privateVar = "java"; } public void getVariable() { System.out.println(privateVar); } public void setVariable(String setvalue) { privateVar = setvalue; } } public class PrivateVariables { public static void main(String[] args) { TestEncapsulation test = new TestEncapsulation(); test.setVariable("PHP"); test.getVariable(); //System.out.println(TestEncapsulation.privateVar); } }
Output
PHP
Advantages of Encapsulation in Java
Following are the advantages of encapsulation in Java.
Protect Your Data
With encapsulation, you can keep your data and codes safe from external inheritance. For example, if any program runner tries to change the program, they can only interact with the getter and setter methods of the program. They will not have any idea to change any specific variable or data and hinder the running of the program resulting in high security.
Easy to Test code
The code which is encapsulated is simple to debug and easy to test for unit testing.
Flexible
The encapsulated code is cleaner, flexible, and easy to change as per our needs. It means we can change the code read-only or write-only by getter and setter methods.
For example, if you don’t define the setter method in the class then the fields can be made read-only whereas if you don’t define the getter method in the class then the fields can be made write-only.
Easy to Reuse
Encapsulation enables you to easily change the methods, reuse the code, and execute new requirements in your program.
Java program explaining encapsulation
class Encapsulate { // private variables declared // these can only be accessed by // public methods of class private String empName; private String empId; private int empAge; // get method for age to access // private variable empAge public int getAge() { return empAge; } // get method for name to access // private variable empName public String getName() { return empName; } // get method for id to access // private variable empId public String getId() { return empId; } // set method for age to access // private variable geekage public void setAge(int newAge) { empAge= newAge; } // set method for name to access // private variable empName public void setName(String newName) { empName= newName; } // set method for id to access // private variable empId public void setId(String newId) { empId= newId; } } public class TestEncapsulation { public static void main(String[] args) { Encapsulate obj = new Encapsulate(); // setting values of the variables obj.setName("Jose"); obj.setAge(27); obj.setId("STS424"); // Displaying values of the variables System.out.println("Emp's name: " + obj.getName()); System.out.println("Emp's age: " + obj.getAge()); System.out.println("Emp's id: " + obj.getId()); // Direct access of empId is not possible // due to encapsulation // System.out.println("emp's id: " + // obj.empName); } }
Output
Emp's name: Jose Emp's age: 27 Emp's id: STS424
In the above program, the class Encapsulate has wrapped up as the variables are set private. The get methods like getAge() , getName() , getId() are set as public, these methods are used to access these variables. The setter methods like setName(), setAge(), setId() are also set as public and are used to set the values of the variables.
Conclusion
We hope from the above discussion now that you’re well aware of what is encapsulation and its various uses and implementation in the Java program. If you like this post, give us a thumbs up that can inspire us to give you more useful technical kinds of stuff.
- Related Articles
- Encapsulation in Java
- What is encapsulation in Java?
- Abstraction vs Encapsulation in Java
- Advantages of naming conventions in Java
- Advantages of using packages in Java
- Advantages of Object cloning in Java
- Encapsulation in C++
- Encapsulation in C#
- What is the difference between abstraction and encapsulation in Java?
- Advantages and disadvantages of arrays in Java
- How to implement the encapsulation concept in JShell in Java 9?
- Explain Encapsulation in PHP.
- Advantages and Disadvantages of Java Sockets
- What is encapsulation in C#?
- What is Encapsulation in Python?
