class Student {
private String name;
Encapsulation in Java;
Encapsulation is one of the fundamental // Getter method
concepts of object-oriented programming public String getName() {
(OOP) in Java. It is the wrapping data return name;
}
(variables) and methods (functions) into a
single unit like a class. It helps to restrict // Setter method
direct access to some of the object's public void setName(String n) {
name = n;
components, which means protecting the }
data from outside interference and misuse. }
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setName("Rahul");
System.out.println("Student Name: " + s.getName());
}
}