Java Encapsulation
Protect the rules around your state.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Encapsulation.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
Encapsulation lets an object control how its state changes. Private fields and meaningful methods can keep invalid values out. Adding getters and setters alone is not the goal; preserving a useful invariant is.
விரிவான விளக்கங்கள்
Encapsulation: Data-ஐ பாதுகாப்போம்
Encapsulation என்பது sensitive data-ஐ users-கிட்ட இருந்து hide பண்றது. இதுக்கு:
- Class attributes-ஐ
private-ஆ declare பண்ணு - Attributes-ஐ access பண்றதுக்கு
getmethods (getters) பயன்படுத்தலாம் - Attributes-ஐ update பண்றதுக்கு
setmethods (setters) பயன்படுத்தலாம்
Encapsulation-ஓட Benefits
- Class attributes-ஐ better control பண்ணலாம்
- Attributes read-only அல்லது write-only-ஆ வையுங்கள்க்கலாம்
- Flexible — code change பண்றது easy
- Data security — invalid data assign ஆகாம தடுக்கலாம்
class BankAccount {
private double balance; // private — direct access blocked
BankAccount(double initial) { this.balance = initial; }
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; }
}
BankAccount acc = new BankAccount(1000);
acc.deposit(500);
acc.withdraw(200);
System.out.println(acc.getBalance()); // 1300.0Encapsulation-ன் main benefit என்ன?
- Code faster-ஆக run ஆகும்
- Data hiding — internal state-ஐ outside access-ல் இருந்து protect செய்யும்
- Code reuse-ஐ enable செய்யும்
- Multiple inheritance-ஐ allow செய்யும்
Reveal answer / விடை
Data hiding — internal state-ஐ outside access-ல் இருந்து protect செய்யும்
Encapsulation data-ஐ `private`-ஆக வைத்து, controlled access-க்கு `public` getters/setters தரும். Object-ன் internal state-ஐ protect செய்வது — bugs குறையும், maintainability கூடும்.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
Score score = new Score();
score.setPoints(80);
System.out.println(score.getPoints());
}
}
class Score {
private int points;
int getPoints() { return points; }
void setPoints(int value) {
if (value < 0 || value > 100) throw new IllegalArgumentException("Use 0 to 100");
points = value;
}
}Predict first. Then reveal the expected output / வெளியீடு
80
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Attempt to assign -1 using setPoints, then restore the valid value.
Reveal the answer and reasoning / விடை
The method throws IllegalArgumentException because the validation rejects a value outside 0 through 100.
தவிர்க்க வேண்டிய தவறு: Do not bypass validation by making the field public. Keep the rule at the boundary where changes happen.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.