Clean • Professional
Encapsulation is one of the four pillars of Object-Oriented Programming (OOP) in Java — along with inheritance, polymorphism, and abstraction.
It is a core concept that helps in protecting data and keeping your code clean, secure, and maintainable.
Encapsulation means binding data (variables) and methods (functions) that operate on that data into a single unit — a class.
In simple words:
Encapsulation = Data Hiding + Controlled Access
This ensures that sensitive data is not directly accessed by outside code — only through public methods (getters and setters).
Example of Encapsulation
class Student {
// Private data - cannot be accessed directly
private String name;
private int age;
// Public getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 0) {
this.age = age;
} else {
System.out.println("Invalid age!");
}
}
}
public class EncapsulationExample {
public static void main(String[] args) {
Student s = new Student();
s.setName("Alice");
s.setAge(20);
System.out.println("Name: " + s.getName());
System.out.println("Age: " + s.getAge());
}
}
Output:
Name: Alice
Age: 20

| Benefit | Description |
|---|---|
| Data Hiding | Prevents direct access to sensitive data. |
| Code Control | Provides controlled access using getter and setter methods. |
| Improved Security | Protects object integrity by validating data. |
| Reusability | Easier to maintain and reuse code. |
| Flexibility | You can change internal implementation without affecting other classes. |
Access modifiers control the visibility or accessibility of classes, methods, and variables.
There are four main access levels in Java:
| Modifier | Scope / Visibility | Can Access From |
|---|---|---|
private | Within the same class only | Only inside the class |
default (no keyword) | Within the same package | Classes in same package |
protected | Same package + subclasses (even in other packages) | Package + inheritance |
public | Accessible from anywhere | All classes everywhere |
Example of Access Modifiers
package mypackage;
public class Person {
private String name; // Accessible only within Person class
int age; // Default access
protected String address; // Accessible in subclasses
public String email; // Accessible everywhere
public void display() {
System.out.println("Name: " + name);
}
}
package otherpackage;
import mypackage.Person;
public class AccessExample extends Person {
public void showAccess() {
// name → private (not accessible)
// age → default (different package)
System.out.println(address); // protected
System.out.println(email); // public
}
}
By combining encapsulation and access modifiers, you can:
public methodsExample:
class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) balance -= amount;
else System.out.println("Insufficient funds!");
}
}
Encapsulation in Java means hiding the internal data of a class and providing controlled access to it through getter and setter methods — commonly known as get() and set() methods.
These methods act like a protective layer between your class fields (data) and the outside world.
| Method Type | Purpose | Typical Naming |
|---|---|---|
| Getter | Reads or returns the value of a private field | getFieldName() |
| Setter | Updates or modifies the value of a private field | setFieldName(value) |
class Student {
// Step 1: Private variables (data hiding)
private String name;
private int age;
// Step 2: Public getter method (to access private data)
public String getName() {
return name;
}
// Step 3: Public setter method (to modify private data)
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) { // basic validation
this.age = age;
} else {
System.out.println("Invalid age!");
}
}
}
public class GetterSetterExample {
public static void main(String[] args) {
Student s = new Student();
s.setName("Aman");
s.setAge(22);
System.out.println("Name: " + s.getName());
System.out.println("Age: " + s.getAge());
}
}
Output:
Name: Aman
Age: 22
| Reason | Explanation |
|---|---|
| Data Hiding | Prevents direct modification of variables from outside. |
| Validation | You can check values before setting (e.g., age > 0). |
| Flexibility | Allows changes in implementation without affecting other code. |
| Read-Only / Write-Only Access | You can choose to expose only getter or setter based on need. |
Read-Only Field (Only Getter):
class Config {
private final String version = "1.0";
public String getVersion() {
return version;
}
}
Write-Only Field (Only Setter):
class Password {
private String secret;
public void setSecret(String secret) {
this.secret = secret;
}
}