Java Notes — Data Encapsulation
Exam Style | Easy English | Short Paragraphs + Key Points | With PPT Code Snippets
(Based on: data [Link])
1) Definition (Very Important)
Data Encapsulation is the process of wrapping data (variables) and methods (functions)
together into a single unit (class) and restricting direct access to the data.
• In simple words: “Data hiding + controlled access = Encapsulation”.
• Data and methods stay together inside a class.
• Direct access to data is restricted.
Exam Line: Encapsulation = Data hiding + controlled access through methods.
2) Why do we use Encapsulation?
Encapsulation is used to protect data and control how data is used. It also makes code more
secure and organized.
• Protects data from unauthorized access.
• Controls how data is used (controlled access).
• Makes code secure and organized.
3) How Encapsulation is achieved in Java?
Encapsulation in Java is achieved by using private variables (to hide data) and public
getter/setter methods (to access data in a controlled way).
• Private variables (data hidden).
• Public getter and setter methods (controlled access).
4) Basic Encapsulation Example (From PPT)
This example shows private fields and public methods to set and get values. Direct access
like [Link] is not allowed, so we must use methods.
class Student {
// Private data (hidden)
private int id;
private String name;
// Setter method (to set data)
public void setData(int i, String n) {
id = i;
name = n;
// Getter method (to get data)
public void getData() {
[Link](id + " " + name);
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
[Link](101, "Rohail"); // setting data
[Link](); // getting data
Explanation (Easy)
• private → data is hidden.
• Cannot access directly like: [Link] (not allowed).
• Must use methods: setData() and getData().
• This controlled access is Encapsulation.
5) Real-Life Example (ATM) + Table Idea
Encapsulation exists in real life when internal data is not directly accessible and we must
use a proper access method.
• ATM: Balance is private data; access is through PIN and options.
• Bank: Account money is protected; access is through withdrawal slip / process.
• Mobile: Internal hardware is hidden; access is through apps/UI.
6) Key Features of Encapsulation
• Data hiding
• Security
• Controlled access
• Better code management
7) Advantages of Encapsulation
• Protects data
• Improves security
• Easy maintenance
• Flexible code
• Prevents misuse
8) Encapsulation vs Data Hiding (Difference)
Encapsulation is a broader concept. Data hiding is part of encapsulation.
• Encapsulation: Wrapping data + methods together.
• Data hiding: Hiding only data.
• Encapsulation: achieved using class.
• Data hiding: achieved using private.
• Encapsulation is broader; data hiding is its part.
9) Basic Encapsulation (Revision Code from PPT)
class Student {
private int id;
private String name;
public void setData(int i, String n) {
id = i;
name = n;
public void showData() {
[Link](id + " " + name);
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
[Link](101, "Rohail");
[Link]();
• private → data hidden
• Access through methods only
• This is Encapsulation
10) Proper Getter & Setter Methods (Standard Style)
Standard Java style uses separate getter and setter methods for each field.
class Employee {
private int empId;
private String empName;
// Setter
public void setEmpId(int id) {
empId = id;
public void setEmpName(String name) {
empName = name;
// Getter
public int getEmpId() {
return empId;
public String getEmpName() {
return empName;
public class Main {
public static void main(String[] args) {
Employee e1 = new Employee();
[Link](500);
[Link]("Ali");
[Link]([Link]());
[Link]([Link]());
Output (From PPT)
500
Ali
// Data accessed in controlled way.
11) Types of Encapsulation (From PPT)
11.1 Read-Only Encapsulation (Read but not modify)
Read-only encapsulation allows reading data but not modifying it (getter only).
class Company {
private String companyName = "ABC Ltd";
public String getCompanyName() {
return companyName;
public class Main {
public static void main(String[] args) {
Company c1 = new Company();
[Link]([Link]());
}
11.2 Write-Only Encapsulation (Set but not view)
Write-only encapsulation allows setting data but not viewing it (setter only).
class Password {
private String pass;
public void setPassword(String p) {
pass = p;
12) Encapsulation with Validation ⭐ (Very Important)
Encapsulation allows validation (data checking) before storing values. This prevents invalid
data from being saved.
class BankAccount {
private double balance;
public void setBalance(double b) {
if (b >= 0) {
balance = b;
} else {
[Link]("Invalid Balance");
public double getBalance() {
return balance;
public class Main {
public static void main(String[] args) {
BankAccount b1 = new BankAccount();
[Link](5000);
[Link]([Link]());
[Link](-200); // invalid
Output (From PPT)
5000.0
Invalid Balance
// Encapsulation protected the data.
• If direct access existed, negative balance could be stored.
• Encapsulation protects data using validation.
13) One-Line Memory Tip
Encapsulation = “Data inside a capsule”
14) Exam / Class Activity (Short Answers)
Q1) Primary purpose of encapsulation?
To protect data and provide controlled access through methods, so data cannot be misused
or accessed directly.
Q2) Standard practice for fields in an encapsulated class?
Declare fields as private and access them using public getter and setter methods.
Q3) How does data hiding improve security?
Because private data cannot be changed directly, only controlled methods can
access/update it, reducing unauthorized changes.
Q4) Benefit during code maintenance?
Internal implementation can change (like adding validation) without changing how other
parts of program use the class (same getters/setters).
Q5) Setter’s responsibility beyond updating value?
A setter can validate or check data before storing it (example: balance cannot be negative).