Encapsulation is one of the four fundamental OOP concepts. It is the process of wrapping code and data together into a single unit, much like a protective capsule. In Java, this is achieved by making class variables private and providing public getter and setter methods to access and modify them.
Imagine a BankAccount class where the balance variable is public. Any external class could set the balance to a negative number or zero without permission. Encapsulation prevents this "data corruption" by acting as a filter.
int to long) without breaking the code of others who use your class.Implementation follows a strict two-step professional standard in Java:
getName()) and setters should start with "set" (e.g., setName()). For boolean variables, the getter usually starts with "is" (e.g., isActive()).
Encapsulation gives you the power to create specialized classes by choosing which methods to provide:
A class that only contains getter methods. Once the object is initialized (usually via a constructor), its data cannot be changed. This is great for configuration settings.
A class that only contains setter methods. This is rare but useful for logging sensitive data where you can "input" information but cannot "read" it back for security reasons.
The real magic of encapsulation happens inside the setter method. It acts as a gatekeeper for your data.
Example Logic: If a user tries to set salary = -5000, your setter can intercept this and either throw an error or default the value to 0. This ensures your object's state is always valid.
This example demonstrates how encapsulation protects data using private fields and validated setters.
While often used interchangeably, there is a subtle difference that senior developers should know:
| Feature | Data Hiding | Encapsulation |
|---|---|---|
| Focus | Restricting access to data members. | Bundling data and methods together. |
| Achievement | Achieved using private modifier. |
Achieved using a class. |
| Benefit | Security and integrity. | Modularity and complexity management. |
Q: Is Java a fully encapsulated language?
A: No. Java provides features for encapsulation, but it’s up to the developer to implement it correctly by using access modifiers. You can still write a class with all public variables (though you shouldn't!).
Q: What is a "POJO"?
A: POJO stands for "Plain Old Java Object." It is a class that follows strict encapsulation—private fields, a no-arg constructor, and public getters/setters.
Q: Does encapsulation affect performance?
A: The impact is negligible. Modern JVMs (Java Virtual Machines) use a technique called "Inlining" where the getter/setter calls are optimized so they perform almost as fast as direct field access.
Encapsulation is about **Integrity**. By shielding your data and controlling how it is accessed, you build software that is robust, easy to maintain, and resistant to accidental bugs. It is the bridge between chaotic code and organized, professional architecture.
Next: Master Java Inheritance →