encapsulation
Encapsulation is one of the fundamental principles of object-oriented programming (OOP) and refers to the concept of bundling data (attributes or properties) and methods (functions) that operate on that data into a single unit called a class. Additionally, it involves controlling access to the internal state of an object, often by using access modifiers, to ensure data integrity and encapsulation of implementation details.
In Dart, encapsulation is achieved through the use of access modifiers and conventions. Here's an explanation of how encapsulation works in Dart:
Public Access Modifier (Default):
In Dart, by default, class members (fields and methods) are public, meaning they can be accessed from anywhere in the code.
class Person { String name; // Public field void introduce() { print('My name is $name.'); } } void main() { var person = Person(); person.name = 'Alice'; // Accessing the public field person.introduce(); // Accessing the public method }
Private Access Modifier (_):
Dart allows you to mark a field or method as private by prefixing its name with an underscore
_
. Private members can only be accessed from within the same library.
class Person { String _name; // Private field Person(this._name); // Private field initialized through constructor void _printName() { print('My name is $_name.'); } void introduce() { _printName(); // Accessing the private method from within the class } } void main() { var person = Person('Alice'); person.introduce(); // Accessing the public method, which accesses the private method // person._name; // This would be an error as _name is private // person._printName(); // This would also be an error as _printName is private }
By using encapsulation and private access modifiers, you can hide the internal details of a class from external code, reducing the risk of unintended interference with the class's state and behavior. This helps improve code maintainability, readability, and robustness, as the class can enforce its own rules for accessing and modifying its data.
Last updated