[go: up one dir, main page]

0% found this document useful (0 votes)
11 views1 page

Oject Oreinted Programming

Encapsulation is a core principle of Object-Oriented Programming that combines data and methods into a class while restricting direct access to the object's components. It offers benefits such as data hiding, modularity, code maintainability, and security. An example in C# illustrates how encapsulation protects internal data, as seen in applications like banking and inventory systems.

Uploaded by

emerybrad4
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)
11 views1 page

Oject Oreinted Programming

Encapsulation is a core principle of Object-Oriented Programming that combines data and methods into a class while restricting direct access to the object's components. It offers benefits such as data hiding, modularity, code maintainability, and security. An example in C# illustrates how encapsulation protects internal data, as seen in applications like banking and inventory systems.

Uploaded by

emerybrad4
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
You are on page 1/ 1

5.

Encapsulation (Object-Oriented Programming)

Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP).


It refers to the bundling of data (variables) and the methods (functions) that operate on that data
into a single unit, or class, and restricting direct access to some of the object’s components.

The main idea is "hide the internal state of an object and require all interaction to be
performed through an object's methods."

Benefits of Encapsulation:

 Data Hiding: Prevents external code from directly accessing internal data, reducing the
chance of accidental corruption.
 Modularity: Changes in the internal implementation of a class do not affect code that
uses the class.
 Code Maintainability: Easier to debug and manage.
 Security: Sensitive data can only be accessed through controlled, defined methods.

Example in C#:

csharp
CopyEdit
public class BankAccount {
private double balance;

public void Deposit(double amount) {


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

public double GetBalance() {


return balance;
}
}

Here, balance is private and can only be changed using the Deposit method. This protects it
from being arbitrarily altered, which is key in secure and reliable software.

Encapsulation is used in building real-world systems like banking apps, ticketing systems,
inventory tracking, etc., where data integrity is critical.

Let me know if you want diagrams, visuals, or real-world examples added to any of these!

You might also like