0% found this document useful (0 votes)
5 views2 pages

Encapsulation

Encapsulation is a key principle of Object-Oriented Programming that involves bundling data and methods into a single unit, typically a class, while restricting direct access to protect internal state. It can be achieved in Java by declaring variables as private and providing public getter and setter methods for controlled access. The benefits of encapsulation include enhanced security, maintainability, flexibility, data validation, and reduced dependencies between application components.

Uploaded by

tarushiag03450
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)
5 views2 pages

Encapsulation

Encapsulation is a key principle of Object-Oriented Programming that involves bundling data and methods into a single unit, typically a class, while restricting direct access to protect internal state. It can be achieved in Java by declaring variables as private and providing public getter and setter methods for controlled access. The benefits of encapsulation include enhanced security, maintainability, flexibility, data validation, and reduced dependencies between application components.

Uploaded by

tarushiag03450
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 is one of the four fundamental principles of Object-Oriented Programming (OOP).

It is the
mechanism of bundling data (variables) and the methods (code) that operate on that data into a single
unit, typically a class.

Key Concepts
 Data Hiding: Restricting direct access to class members to protect internal state.
 Single Unit: Variables and methods are wrapped together like a "capsule".
 Controlled Access: Interaction with data happens only through predefined interfaces (methods).

How to Achieve Encapsulation


To implement encapsulation in Java, follow these two steps:

1. Declare variables as private : This prevents direct access from outside the class.
2. Provide public Getter and Setter methods: These act as the bridge to view or modify the private
data.

Code Example

public class Employee {


// 1. Private variables (Data Hiding)
private String name;
private double salary;

// 2. Public Getter (Access)


public String getName() {
return name;
}

// 3. Public Setter with Validation (Control)


public void setSalary(double newSalary) {
if (newSalary > 0) {
[Link] = newSalary;
}
}
}

Benefits of Encapsulation
 Security: Protects sensitive data from unauthorized or accidental modification.
 Maintainability: Internal implementation can change without breaking external code.
 Flexibility: You can make a class read-only (only getters) or write-only (only setters).
 Data Validation: Setters allow you to check if the data being entered is valid before saving it.
 Loose Coupling: Reduces dependencies between different parts of the application.

Encapsulation vs. Abstraction


Feature Encapsulation Abstraction
Focus How it is done (Implementation) What it does (Essential features)
Goal Hiding data (Data Security) Hiding complexity
Method Access modifiers (private, public) Abstract classes and Interfaces

You might also like