In the object-oriented world, we have pillars of programming. One of them is Encapsulation the other three are Inheritance, polymorphism, and abstraction.
Here's the article which will explain, What, Why, and How of Encapsulation, and the other three are yet to be released one by one. So let’s get started with encapsulation.
Introduction
According to most of the resources, encapsulation means hiding the data members (variables and methods) within the class or a mechanism of wrapping/hiding a code and the variables/methods together as a unit.
In this, we will come across the entire concept of what encapsulation means, why to use encapsulation and how to achieve encapsulation in the programming world.
Data Hiding Vs Encapsulation
When encapsulation is implemented, only the variables inside the class can access it.
Therefore it's known as data hiding. But encapsulation and data hiding both are different from each other.
Data Hiding means protecting the members of a class from unauthorized/illegal access.
Encapsulation automatically achieves the concept of data hiding by providing security to data by making the variables as private.
As it is difficult to understand encapsulation through definitions we will learn in depth about this through some code examples,
- package Encapsulation;
- //Use of POJO (Plain Old Java Object) class
- public class Employee {
- //Member Variables
- String name;
- int age;
- String address;
- void code() {}
- void role() {}
- void employer() {}
- }
Here the common behavior and characteristics of an employee are enclosed in a single unit, the Employee.java class. This is a process of encapsulation.
An Employee object exposes its behavior and characteristics to the outside world.
- Employee emp = new Employee();
- emp.name = "Aman";
- emp.role();
Here encapsulation hides implementation details of the Employee class. Similarly creating an interface is also a process of encapsulation.
- package Encapsulation;
- interface Employee {
- void code();
- void role();
- void employer();
- }
How to achieve encapsulation?
- Encapsulation is implemented in java using interfaces, class, access modifiers, setters, and getters.
- A Class/Interface will encapsulate essential features of an object.
- Access Modifiers such as private, public, protected restrict access to data at different levels.
- Setter/Getter methods prevent data from misuse or unwanted changes.
For achieving encapsulation in java we declare the variable of a class as private. Private data members are used because they are only accessible within the class. It can’t be accessed using outside the class, the only way to access it is to use setter/getter methods.
Let’s modify the Employee. class to prevent the member variables name, age, and address from being modified by other objects.
- package Encapsulation;
- //Use of POJO (Plain Old Java Object) class
- public class Employee {
- private String name;
- private int age;
- private String address;
- }
Here’s the field name, age, and address that can be changed only in the Employee. class. If someone attempts to make a change like shown below, then code will not be compiled as the field is marked as private.
- Employee emp = new Employee();
- emp.name = "Aman" ; //Compile Time Error
But what if we want to modify/view the attributes values in a safer manner, then we use getter/setter methods. Creating a setter for field age,
- public void setAge(int age) {
- if (age < 18 || age > 58) {
- throw new IllegalArgumentException("Age must belong to 18 to 58 only");
- }
- this.age = age;
- }
Here, one can change the age of the Employee object and set it to a valid range. Likewise, with the name field associated with an Employee object, one can change the details but can’t enter null or empty values as the setter will throw an exception.
- public void setName(String name) {
- if (name.equals(null)) {
- throw new IllegalArgumentException("name cannot be null");
- }
- this.name = name;
- }
This protest data from unauthorized access or malicious changes, for example:

Join the conversation! Your thoughts help the community grow.