Encapsulation in Java
A Simple Thought
Think of your school report card. You can see your marks, but you can’t change them yourself — only the teacher can.
That’s how encapsulation works in programming. It’s a way to protect important data by keeping it hidden inside the class and allowing access only through controlled methods.
Encapsulation helps prevent accidental changes, keeps your program secure, and makes your code easier to manage.
In this lesson, you’ll learn how to protect and access data safely using encapsulation in Java.
The Project: Bank Account Manager
You will create a simple program that represents a bank account.
The program will keep the account balance private so it cannot be changed directly, but it will allow deposits and balance checks using special methods.
Your output will look like this:
1 2 3 4 5 6 7======================== BANK ACCOUNT MANAGER Depositing 1000... Current Balance: 1000 Depositing 500... Current Balance: 1500 ========================
Only the deposit method will be allowed to update the balance, not direct access.
The Concept: Encapsulation
Encapsulation means bundling data and methods together while keeping the data private from outside access.
You use the private keyword to hide variables and provide public methods to read or modify them safely.
Here’s a basic example:
1 2 3 4 5 6 7 8 9 10 11class BankAccount { private int balance = 0; public void deposit(int amount) { balance = balance + amount; } public int getBalance() { return balance; } }
In this class:
- balance is private, so it cannot be accessed directly from outside the class.
- The deposit method changes the balance safely.
- The getBalance method allows controlled access to read it.
This way, only trusted methods can touch the sensitive data.
Why This Matters
Encapsulation is one of the main principles of object-oriented programming.
It helps you:
- Protect data from unintended changes
- Maintain clean, predictable code
- Make debugging easier
- Keep classes self-contained and secure
Real-world systems, such as banking software or online accounts, use encapsulation to make sure important data stays protected.
Let’s Build the Bank Account Program
Here’s the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29class BankAccount { private int balance = 0; public void deposit(int amount) { System.out.println(" Depositing " + amount + "..."); balance = balance + amount; } public int getBalance() { return balance; } } public class BankManager { public static void main(String[] args) { System.out.println("========================"); System.out.println(" BANK ACCOUNT MANAGER"); BankAccount account = new BankAccount(); account.deposit(1000); System.out.println(" Current Balance: " + account.getBalance()); account.deposit(500); System.out.println(" Current Balance: " + account.getBalance()); System.out.println("========================"); } }
When you run this program, you can see how the balance changes after each deposit, but you cannot modify it directly from outside the class.
Encapsulation keeps the balance safe from accidental or unauthorized changes.
How It Works
- The variable balance is declared as private, meaning it’s hidden from outside the class.
- The method deposit() updates the balance only through controlled access.
- The method getBalance() provides a safe way to read the balance.
- The main method can only use these two public methods, not change balance directly.
Encapsulation ensures that only the class itself can decide how its data is handled.
Learn Together with AI
If you are using an AI Copilot, you can learn encapsulation step by step.
Try this prompt:
“Write a Java program that shows encapsulation using a BankAccount class with private balance and public deposit and getBalance methods.”
Then expand your learning with new prompts:
- “Add a method to withdraw money safely, making sure balance never goes below zero.”
- “Add a method to set an account holder’s name.”
- “Display an error if someone tries to deposit a negative amount.”
Each experiment will help you understand how encapsulation keeps your data safe and consistent.
Practice Challenge
Create a program that uses encapsulation to manage student marks.
The class should keep the marks private and allow updates and access through methods.
Your output should look like this:
1 2 3 4 5 6 7======================== STUDENT MARKS MANAGER Adding 85 marks... Current Marks: 85 Adding 10 marks... Current Marks: 95 ========================
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29class Student { private int marks = 0; public void addMarks(int value) { System.out.println(" Adding " + value + " marks..."); marks = marks + value; } public int getMarks() { return marks; } } public class MarksManager { public static void main(String[] args) { System.out.println("========================"); System.out.println(" STUDENT MARKS MANAGER"); Student student1 = new Student(); student1.addMarks(85); System.out.println(" Current Marks: " + student1.getMarks()); student1.addMarks(10); System.out.println(" Current Marks: " + student1.getMarks()); System.out.println("========================"); } }
When you run this, the marks are updated and displayed using safe, controlled methods.
The private variable cannot be changed directly, which protects the student’s data.
Coming Up Next
You have learned how encapsulation keeps data safe and organized inside a class.
Next, you will explore polymorphism, another key concept in object-oriented programming, which allows the same method to behave differently depending on the situation.