0% found this document useful (0 votes)
7 views5 pages

Encapsulation in Java

Encapsulation is a fundamental concept in Java's Object-Oriented Programming that combines data and methods into a single unit while restricting access to the data. It enhances data security, validation, flexibility, and maintainability through the use of access modifiers and getter/setter methods. Although it increases code length and requires additional methods, encapsulation is crucial for protecting sensitive information and reducing complexity.

Uploaded by

davidmule33
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Encapsulation in Java

Encapsulation is a fundamental concept in Java's Object-Oriented Programming that combines data and methods into a single unit while restricting access to the data. It enhances data security, validation, flexibility, and maintainability through the use of access modifiers and getter/setter methods. Although it increases code length and requires additional methods, encapsulation is crucial for protecting sensitive information and reducing complexity.

Uploaded by

davidmule33
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Encapsulation in Java

1️⃣ Introduction to Encapsulation


Encapsulation is one of the four pillars of Object-Oriented Programming (OOP) in Java:

 Encapsulation
 Inheritance
 Polymorphism
 Abstraction

📘 Definition

Encapsulation is the process of binding data (variables) and methods (functions) together
into a single unit (class) and restricting direct access to the data from outside the class.

It is also known as data hiding.

2️⃣ Why Encapsulation is Important


Encapsulation provides:

🔒 1. Data Security

 Prevents unauthorized access to class variables.


 Protects sensitive information.

🧪 2. Data Validation

 Allows checking conditions before assigning values.


 Prevents invalid or harmful data.

🔄 3. Flexibility & Maintainability

 Internal implementation can change without affecting other classes.


 Code becomes easier to maintain.

🧩 4. Better Control

You can:

 Make variables read-only (only getter)


 Make variables write-only (only setter)
 Make variables read-write (both getter and setter)

3️⃣ How Encapsulation is Achieved in Java


Encapsulation is achieved using:

1. Access Modifiers
2. Getter and Setter Methods

4️⃣ Access Modifiers in Encapsulation


Modifier Access Level
private Only within the same class
default Same package
protected Same package + subclasses
public Everywhere

👉 For encapsulation, we mainly use private variables.

5️⃣ Basic Structure of Encapsulation


class ClassName {
private dataType variable; // hidden data

public void setVariable(dataType value) { // setter


[Link] = value;
}

public dataType getVariable() { // getter


return variable;
}
}

6️⃣ Detailed Example with Validation


class BankAccount {

private String accountHolder;


private double balance;

// Setter with validation


public void setAccountHolder(String name) {
[Link] = name;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
}
}

// Getter
public double getBalance() {
return balance;
}
}

Explanation:

 balance is private → cannot be directly modified.


 User must use deposit() or withdraw().
 Invalid transactions are prevented.

7️⃣ Encapsulation Flow (Conceptual Diagram)


Outside Class → Public Methods → Private Data
(User) (Gateway) (Protected)

OR

[ User ]

[ Getter / Setter Methods ]

[ Private Variables ]

The user never directly accesses the private data.

8️⃣ Real-Life Examples of Encapsulation


🏧 ATM Machine

 Balance is hidden.
 You use withdraw(), deposit(), checkBalance().

🚗 Car
 Engine details are hidden.
 You use methods like start(), accelerate().

👨‍🎓 Student System

 Marks are private.


 You use setMarks() and getMarks().

9️⃣ Types of Encapsulation


1️⃣ Fully Encapsulated Class

All variables are private.

Example:

class Student {
private int id;
private String name;
}

2️⃣ Partially Encapsulated Class

Some variables are private, others are public.

🔟 Advantages of Encapsulation
✔ Improves security
✔ Supports data validation
✔ Reduces complexity
✔ Increases flexibility
✔ Easy to test and maintain
✔ Helps achieve abstraction

1️⃣1️⃣ Disadvantages of Encapsulation


❌ Slight increase in code length
❌ Requires writing getter and setter methods
1️⃣2️⃣ Encapsulation vs Abstraction (Detailed Difference)
Encapsulation Abstraction
Hides data Hides implementation
Achieved using private variables Achieved using abstract classes & interfaces
Focus on data security Focus on design
Uses getters/setters Uses abstract methods

1️⃣3️⃣ Important Keywords


 private
 public
 this
 Getter
 Setter
 Access Modifiers
 Data Hiding

You might also like