Encapsulation in Java is a fundamental concept in object-oriented programming (OOP) that refers to the idea of binding data and methods that operate on that data into a single unit, called a class. It is a key mechanism for data hiding, which means that the internal details of an object are not visible to the outside world.
Table of Contents
Encapsulation Implementation using Access Modifiers
In Java, encapsulation is implemented using access modifiers, which determine the level of access that the data and methods of a class have. There are four access modifiers in Java:
- Public: The data and methods with this modifier are accessible from anywhere in the code, even outside the class.
- Private: The data and methods with this modifier are only accessible from within the class. They cannot be accessed from outside the class, even by the subclasses of the class.
- Protected: The data and methods with this modifier are accessible from within the class and its subclasses, but not from outside the class hierarchy.
- Default: The data and methods with no access modifier (i.e., no modifier is specified) are only accessible from within the same package as the class.
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public class Main {
public static void main(String[] args) {
Person person = new Person("John Doe", 30);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
person.setName("Jane Smith");
person.setAge(35);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}
}
Output

Advantages of Encapsulation in Java
Improved data security: Encapsulation improves the security of the code by controlling the access to the data and methods of a class. This means that only the authorized users can access the data and methods, which prevents unauthorized modification of the data.
Improved code maintainability: Encapsulation improves the maintainability of the code by reducing the complexity and increasing the modularity of the code. This makes it easier to maintain and extend the code.
Improved code reusability: Encapsulation improves the reusability of the code by making the code more modular and independent of other classes. This makes it easier to reuse the code in different contexts.
Encapsulation vs. Abstraction
- Encapsulation and abstraction are two fundamental concepts in object-oriented programming.
- Encapsulation refers to the mechanism of binding data and methods within a class, while abstraction refers to the mechanism of hiding the implementation details of a class from the users of the class.
- Encapsulation enables data hiding and access control, which improves the security, maintainability, and reusability of the code.
- Abstraction enables the development of modular and extensible applications by hiding the implementation details of a class and exposing only the essential features to the users of the class.
- Encapsulation is a key mechanism for achieving abstraction, as it enables the implementation details of a class to be hidden from the users of the class.
Conclusion
Encapsulation in Java enables data hiding and abstraction, ensuring that access to data and methods is controlled through access modifiers, improving security, maintainability, and reusability. This results in modular and extensible applications.