Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 22

Encapsulation in Java

In this lesson: Hide data with private fields and public getters/setters in Java — benefits and examples with output. In Hindi & English.

What is Encapsulation?

Encapsulation means wrapping the data (variables) and the code (methods) that work on that data into a single unit — a class — and hiding the internal details from the outside world. In Java it is achieved by making fields private and giving controlled access through public getter and setter methods.

💡 Real-world analogy

A medicine capsule wraps the medicine inside a shell — you can't touch the powder directly. Similarly, encapsulation wraps data inside a class and you access it only through methods.

How to Achieve Encapsulation

  • Declare the class fields as private.
  • Provide public getter methods to read them.
  • Provide public setter methods to change them (with validation).
Java
class Account {
    private double balance;   // hidden data

    public double getBalance() {   // getter
        return balance;
    }
    public void deposit(double amt) {  // controlled setter
        if (amt > 0) balance += amt;   // validation!
    }
}
public class Main {
    public static void main(String[] a) {
        Account ac = new Account();
        ac.deposit(500);
        System.out.println(ac.getBalance());
    }
}
▶ Output
500.0

Benefits of Encapsulation

BenefitWhy it matters
Data hidingOutside code can't directly change balance to an invalid value.
ControlThe setter can validate input (no negative deposit).
FlexibilityYou can change the internal code later without breaking other classes.
Read-only / write-onlyProvide only a getter (read-only) or only a setter (write-only) as needed.
⚠️ Common mistake

Making fields public defeats encapsulation — anyone can then set balance = -9999. Always keep fields private.

Summary

  • Encapsulation = data + methods in one class, with internal details hidden.
  • Achieved by private fields + public getters/setters.
  • Gives data hiding, validation, flexibility and controlled access.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicException Handling Your progress: 22 / 61 (36%) · Lesson 23 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free