Encapsulation is one of the fundamental concepts in object-oriented programming (OOP) and plays a vital role in Java programming. It refers to the bundling of data (variables) and the methods (functions) that operate on that data into a single unit, or class. In Java, encapsulation is implemented using access modifiers (like private
, protected
, and public
), which control the visibility of the data and methods.
Encapsulation helps in hiding the internal state of an object and protects it from unwanted external access. This not only provides a layer of security but also promotes better maintenance of the code.
In this guide, we will explore what encapsulation is, how it works in Java, its advantages, and practical examples.
Encapsulation is the practice of hiding the internal details of an object and restricting access to some of its components. The primary goal of encapsulation is to protect the integrity of the object by ensuring that its internal state is not directly modified from outside the class. Instead, access to the data is controlled through getter and setter methods, which allow safe access and modification.
By doing so, encapsulation allows for:
Encapsulation in Java is achieved using:
private
to restrict direct access from outside the class.
class ClassName {
// Private variables
private String name;
private int age;
// Public getter method for 'name'
public String getName() {
return name;
}
// Public setter method for 'name'
public void setName(String name) {
this.name = name;
}
// Public getter method for 'age'
public int getAge() {
return age;
}
// Public setter method for 'age'
public void setAge(int age) {
if(age > 0) { // Validation inside setter
this.age = age;
}
}
}
In this example:
name
and age
variables are private.Access modifiers determine the level of access to the members of a class. Java provides several access modifiers to implement encapsulation effectively:
private
: The member is accessible only within the same class.default
(no modifier): The member is accessible only within the same package.protected
: The member is accessible within the same package and by subclasses (even if they are in different packages).public
: The member is accessible from anywhere in the program.
class Employee {
// Private member (not accessible directly outside this class)
private String name;
// Public member (accessible from anywhere)
public int salary;
// Default member (accessible within the same package)
int age;
// Protected member (accessible within the package and subclasses)
protected String department;
// Getter and setter methods for encapsulation
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here, name
is private, salary
is public, age
has default access, and department
is protected.
Let's now look at a practical example where we create a Student
class with private fields and provide public methods for accessing and modifying the fields.
class Student {
// Private variables
private String studentName;
private int studentAge;
// Constructor to initialize the object
public Student(String name, int age) {
this.studentName = name;
this.studentAge = age;
}
// Getter method for studentName
public String getStudentName() {
return studentName;
}
// Setter method for studentName
public void setStudentName(String studentName) {
this.studentName = studentName;
}
// Getter method for studentAge
public int getStudentAge() {
return studentAge;
}
// Setter method for studentAge
public void setStudentAge(int studentAge) {
if(studentAge > 0) {
this.studentAge = studentAge;
} else {
System.out.println("Age cannot be negative");
}
}
}
public class Main {
public static void main(String[] args) {
// Creating a Student object
Student student = new Student("John Doe", 20);
// Accessing and modifying the student details via getter and setter methods
System.out.println("Student Name: " + student.getStudentName());
System.out.println("Student Age: " + student.getStudentAge());
// Updating student age
student.setStudentAge(25);
System.out.println("Updated Age: " + student.getStudentAge());
}
}
Output:
Student Name: John Doe
Student Age: 20
Updated Age: 25
In this example, the studentName
and studentAge
fields are private. The user interacts with the fields only through the getter and setter methods.
get
(e.g., getName()
) and setter methods with set
(e.g., setName()
).