What is Encapsulation?
Encapsulation is one of the most important concepts of Java (programming language) object-oriented programming. It means wrapping data (variables) and code (methods) together into a single unit, usually within a class.
In simple words, encapsulation helps protect data from direct access. Instead of changing variables directly, we use methods to control how data is read or updated.
Why Encapsulation is important
Encapsulation gives many benefits
- protects data from unwanted changes
- improves security
- makes code easy to maintain
- controls how data is used
For example, if a student's age should never be negative, encapsulation allows us to check the value before storing it.
What is a POJO Class?
A POJO stands for Plain Old Java Object.
A POJO (Plain Old Java Object) is a simple Java class that contains private instance variables and provides public getter and setter methods to access and modify those variables. This encapsulation ensures that the internal state of the object cannot be accessed or modified directly by other classes, promoting better data security and maintainability.
Key Characteristics of a POJO
- No requirement to extend or implement special classes/interfaces
- Uses private variables (fields)
- Provides public getters and setters
- Can have constructors
- Contains business logic if needed
Private Variables
As private has scope within the class only, so any outside class can't access these variables.
private int id;
private String name;
Setter Methods
setter method is used to set value to these private variables. we can add different logical implementation as per requirement
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
Getter Methods
getter method is used to get data from these private variable
public int getId() {
return id;
}
public String getName() {
return name;
}
Full Implementation
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
There are multiple pointer to remember here
- In a POJO, variables can be initialized using constructors, ensuring that required fields are set at the time of object creation without relying on setter methods.
- If a variable is intended to be immutable or assigned only once, its setter can be omitted and the value can be provided through the constructor, promoting immutability.
- For fields that require updates, setter methods can include validation logic (such as null checks or business rules) to ensure controlled and safe modifications.
- Logging can also be integrated within setters or update methods to track state changes, improving traceability, debugging, and auditability.
- Additionally, POJOs follow encapsulation by keeping fields private and exposing controlled access, making the code more maintainable, reusable, and testable across different layers like DTOs, entities, and API models in microservices architectures.
How to achieve Encapsulation using POJO ?
To achieve encapsulation in Java:
- Declare variables as private
- Provide public getter and setter methods
Example Program
class Student {
private int age;
public void setAge( int a) {
age = a;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.setAge(20);
System.out.println("Age: " + s.getAge());
}
}
Explanation
- private int age; - age cannot be accessed directly outside the class
- setAge() - used to assign value
- getAge() - used to read value
This means data is controlled through methods instead of direct access.
RealLife Analogy with Encapsulation
Think of an ATM machine
You cannot directly touch the money inside.
You use buttons and options to withdraw money.
That is encapsulation:
- Data = money inside machine
- Methods = buttons used to access it
Conclusion
Encapsulation makes java programs secure and organized.
It is a basic but powerful concept in object-oriented programming, and every java
developer should understand it well