Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Encapsulation

Monday, June 17, 2024 12:35 PM

Encapsulation is one of the fundamental principles of object-oriented programming (OOP) and


is essential for creating well-structured and maintainable code. It refers to the bundling of data
(attributes or properties) and methods (functions or procedures) that operate on the data into a
single unit, known as a class. Encapsulation helps in hiding the internal state and
implementation details of an object from the outside world, thus promoting modularity and
reusability of code.
Key Concepts of Encapsulation:
1. Class and Object
- Class : blueprint or template that defines the structure (attributes) and behavior
(methods) of objects.
- Object : instance of a class that encapsulates its own state (data) and behavior
(methods).
2. Access Modifiers
- Dart supports three levels of access modifiers to control access to class members (fields
and methods):
- `public`: Accessible from anywhere.
- `private`: Accessible only within the same library.
- `protected`: Not directly supported in Dart; instead, use conventions or underscore
prefix for private fields.
class Person {
String name; // Public by default

int _age; // Private (accessible only within this class)

void _privateMethod() {
// Private method
}

void publicMethod() {
// Public method
}
}
3. Benefits of Encapsulation

- Data Hiding: Internal details of an object are hidden from the outside world, providing
a clear and manageable interface for interacting with the object.

- Modularity: Classes encapsulate data and methods related to a specific entity,


promoting modularity and reducing complexity.

- Code Reusabilit: Encapsulation allows classes to be reused in different contexts without


modification, as long as the interface remains consistent.

- Flexibility and Maintainability: Encapsulated code is easier to maintain and update


because changes can be localized to the class implementation, minimizing impact on other
parts of the codebase.

4. Getters and Setters


- Dart provides implicit getters and setters for public instance variables. You can also
define custom getters and setters to encapsulate access to private fields.
class Person {
String _name; // Private field

Dart(OOP) Page 1
String get name => _name; // Getter

set name(String value) {


if (value.isNotEmpty) {
_name = value;
}
} // Setter
}

5. Encapsulation Example:
class BankAccount {
String _accountNumber; // Private field
double _balance; // Private field

BankAccount(this._accountNumber, this._balance); // Constructor

void deposit(double amount) {


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

void withdraw(double amount) {


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

double getBalance() {
return _balance;
}
}

void main() {
var account = BankAccount('123456', 1000.0);

print('Initial balance: ${account.getBalance()}'); // Output:


Initial balance: 1000.0

account.deposit(500.0);
print('Balance after deposit: ${account.getBalance()}'); //
Output: Balance after deposit: 1500.0

account.withdraw(200.0);
print('Balance after withdrawal: ${account.getBalance()}'); //
Output: Balance after withdrawal: 1300.0
}
In this example:
- `BankAccount` class encapsulates `_accountNumber` and `_balance` fields with
methods `deposit()` and `withdraw()` controlling access to `_balance`.
- The `_balance` is accessed and updated through methods, ensuring proper
encapsulation and data integrity.

Dart(OOP) Page 2

You might also like