In OOPs, encapsulation is one of the main fundamental principles. It refers to a bundle of data with methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, thereby preventing direct access to them by unauthorized parties.
What is Encapsulation?
They are Data encapsulation is the most important future of a class The functions used in a class can only access the data items Theme functions provide an interface between the data items of the objects and the calling program.
Such insulation of data, which cannot be accessed directly outside class premises although they are available in the same program, is known as Data Hiding.

Think of encapsulation as a Pokeball. Everyone knows how to use it, regardless of how it works on the inside. The Pokemon trainer doesn’t have to know why it works, they just need to know how to throw it and play by the rules. In programming, the user doesn’t have to know anything other than the interface you provide, and you only want to provide them with access to relevant information. In this, a Pokeball has both a home for Pokemon and Pokemon. This is called information hiding and it is important.
Let us take another example that is related to the real world, A TV set operated by remote control. Various functions of the TV set are linked or tied with the remote signals. A specific function in the TV set is activated as a button of the remote control is pressed The TV set can not be controlled by the remote control of other TV sets Now, you can say that the activities of a TV set are merged or grouped with the remote signals of a specific remote control.
In Object Oriented Programming (OOP) data is the fact of central attraction. In order to keep the data secured from outer interference they sm grouped along with the functions available In the class. Such grouping does not allow the data to be accessed outside the class.
The system of wrapping data and function into a single unit (called class) is known as encapsulation. Encapsulation is the process of encapsulating something in a capsule.
That is, to confine all the relevant activities and data of an object within that object. It protects data and code from outside interference.
Encapsulation is a key feature of object-oriented programming. Encapsulation refers to the ability to package related behavior into an object bundle and control or restrict their access to both functions and data from other objects.
It refers to the combination of data and the processes that operate on it. Encapsulation is the technique of limiting unauthorized access to the values or state of structured data objects within a class.
Basically, encapsulation is the process of separating the elements of an abstraction that form its structure and behavior; Encapsulation serves to separate the contractual interface of an abstraction and its implementation.
class Pokemon {
// private field
private String power;
// getter method
public String getpower() {
return power;
}
// setter method
public void setpower(String power) {
this.power = power;
}
}
class Main {
public static void main(String[] args) {
// create an object of Pokemon
Pokemon p1 = new Pokemon();
// change power using setter
p1.setpower("Fire");
// access power using getter
System.out.println("Pokemon has " + p1.getpower() +" Power ");
}
}
Output:
Pokemon has Fire Power
In the above example, we have a private
field Power. Since it is private, it cannot be accessed from outside the class.
In order to access Power, we have used public
methods: getPower()
and setPower()
. These methods are called getter and setter methods.
Making age private allowed us to restrict unauthorized access from outside the class. This is data hiding.
What are the types of Encapsulation?
There are three basic techniques for encapsulating data in object oriented programming:
- Member Variable Encapsulation
- Function Encapsulation
- Class Encapsulation
What are the advantages of Encapsulation?
The following are the main advantages of encapsulation:
- Encapsulation increases code reusability.
- Encapsulation reduces the complexity of the system.
- It provides better code maintainability.
- Encapsulation enables greater flexibility.
- Encapsulation is easy to reuse.
- Encapsulation provides data protection and information hiding.
Conclusion
Encapsulation in OOPs is the concept of binding fields (object state) and methods (behavior) together as a unit. Programming languages such as Java use encapsulation in the form of classes. A class allows the programmer to create an object with variables (data) and behavior (methods or functions).