Modifiers & Encapsulation ๐ก๏ธ¶
Mentor's Note: Encapsulation is about Safety. You don't let anyone touch the internal circuits of your phone, right? You only let them use the screen and buttons. In Java, we do the same with data! ๐ก
๐ The Scenario: The Smartphone Battery ๐¶
Imagine your smartphone.
- The Data (The Battery): The battery is deep inside the phone. It is Private. ๐คซ
- The Access (The Charger): You can't touch the battery directly, but you can plug in a charger (The Setter) or check the battery percentage (The Getter). ๐
- The Result: This prevents you from accidentally poking the battery and causing an explosion! โ
๐ Concept Explanation¶
1. Access Modifiers (The Guard)¶
Modifiers control who can see or use your classes, variables, and methods. - Public: Anyone can see it. ๐ - Private: Only the class itself can see it. ๐ - Protected: Only the family (Subclasses) and neighbors (Same package) can see it. ๐ช
2. Encapsulation (The Vault)¶
Encapsulation is the practice of bundling data (variables) and methods together and hiding the internal details.
- How?: Make variables private and provide public Getters and Setters.
๐จ Visual Logic: The Protective Layer¶
graph LR
subgraph Object: ["Encapsulated Object ๐ฆ"]
A[private data: batteryLevel]
B[public method: getBattery%]
C[public method: charge()]
end
User -- Calls --> B
User -- Calls --> C
User -- X Cannot touch X --> A
๐ป Implementation: The Security Lab¶
// ๐ Scenario: A Secure Person Account
// ๐ Action: Using private fields and public methods
class Person {
private String name; // ๐ Restricted access
// ๐ฅ Getter: To see the value
public String getName() {
return name;
}
// ๐ค Setter: To change the value safely
public void setName(String newName) {
this.name = newName;
}
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
// myObj.name = "John"; // โ ERROR: name has private access
myObj.setName("John"); // โ
Correct way
System.out.println(myObj.getName()); // โ
Prints: John
}
}
๐ Sample Dry Run (Access Control)¶
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | โ | โ | โ | โ |
| protected | โ | โ | โ | โ |
| default | โ | โ | โ | โ |
| private | โ | โ | โ | โ |
๐ Technical Analysis¶
- Why use Encapsulation?:
- Better Control: You can validate data in the Setter (e.g., check if age is positive).
- Security: Hide sensitive internal logic. ๐ก๏ธ
- Flexibility: You can change the internal data type without affecting anyone using your class.
๐ฏ Practice Lab ๐งช¶
Task: The Bank Account
Task: Create a class BankAccount with a private balance. Provide a deposit() method and a getBalance() method.
Goal: Prevent anyone from setting a negative balance! ๐ก
๐ก Interview Tip ๐¶
"Interviewers love asking: 'What is the difference between Abstraction and Encapsulation?' Answer: Encapsulation is about hiding the data, while Abstraction is about simplifying the complexity."
๐ก Pro Tip: "Always start by making everything private. Only open up (make public) what is absolutely necessary!" - Vishnu Damwala
โ Back: Constructors & 'this' | Next: Inheritance (IS-A) โ