Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP) in Java. It allows you to hide the internal state of an object and expose only controlled access through methods.
π What is Encapsulation?
- Definition: Encapsulation is the practice of wrapping data (fields) and methods into a single unit (class) while restricting direct access to some of the object's components.
- Why it matters: Promotes data integrity, security, and easier maintenance.
- When to use: Always for classes that hold critical or sensitive data.
[Related: link-to-other-article]
πΉ How Encapsulation Works
- Declare fields as privateto restrict direct access.
- Provide publicgetter and setter methods to access and modify data safely.
πΉ Example of Encapsulation
class BankAccount {
    private double balance; // Data hidden
    public double getBalance() { // Getter
        return balance;
    }
    public void setBalance(double balance) { // Setter
        if (balance >= 0) {
            this.balance = balance;
        } else {
            System.out.println("Invalid balance amount");
        }
    }
}
Usage:
public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        account.setBalance(1000);
        System.out.println(account.getBalance());
    }
}
πΉ Real-World Analogy
Think of encapsulation like an ATM machine. You donβt access the bankβs internal database directly; instead, you use an interface (buttons) that controls how you interact with your account.
πΉ Getters and Setters Best Practices
- Validate data inside setters to maintain integrity.
- Keep getters simple without heavy logic.
- Use naming convention: getFieldName()andsetFieldName().
π« Common Mistakes and Anti-Patterns
- β Making fields public and bypassing getters/setters.
- β Adding unnecessary getters/setters for every field without considering design.
- β Performing complex business logic inside getters/setters.
π Performance and Memory Implications
- Encapsulation adds negligible overhead.
- Provides long-term performance benefits through maintainable code and fewer bugs.
| Aspect | Impact | 
|---|---|
| Data Hiding | Improves code safety | 
| Getters/Setters | Minimal runtime overhead | 
πΉ When to Use and When to Avoid
- β Use when: Protecting sensitive data, maintaining class invariants, or providing controlled access.
- β Avoid when: Creating simple data holder classes (consider using records in Java 16+).
π§ Best Practices
- Always keep critical fields private.
- Provide only necessary getters and setters.
- Combine encapsulation with immutability where possible.
π Interview Questions
- 
  Q: What is the difference between encapsulation and abstraction? 
 A: Encapsulation hides data implementation, abstraction hides implementation details of methods.
- 
  Q: Can a class be encapsulated without setters? 
 A: Yes, if you want a read-only class.
- 
  Q: Is encapsulation possible without access modifiers? 
 A: No, access modifiers are key to implementing encapsulation.
π Java Version Relevance
| Java Version | Change | 
|---|---|
| Java 1.0 | Encapsulation supported from the beginning | 
| Java 16 | Introduced recordclasses that provide built-in encapsulation | 
β Conclusion & Key Takeaways
- Encapsulation ensures data safety and better maintainability.
- Use private fields with public getters and setters.
- Combine encapsulation with validation for robust design.
β FAQ
Q: Do I need getters and setters for every field?
  A: No, only expose what is necessary.
Q: Does encapsulation make code slower?
  A: No, the overhead is negligible compared to its benefits.
Q: Can final classes still use encapsulation?
  A: Yes, final affects inheritance, not encapsulation.
