Dart – 12 – Encapsulation

Encapsulation in Dart Programming

Encapsulation is one of the fundamental principles of object-oriented programming (OOP), promoting the idea of bundling data (attributes) and methods (functions) into a single unit known as a class. In Dart, a modern and expressive programming language, encapsulation is a key concept that fosters data protection, code organization, and code modularity. In this discussion, we’ll explore the significance of encapsulation in Dart, understand how it works, and see practical examples of its use.

Understanding Encapsulation

Encapsulation is the concept of restricting access to certain details of an object and only exposing the necessary parts. This helps maintain the integrity of the data and ensures that it is accessed and modified through controlled interfaces. Encapsulation is often achieved through access modifiers, which define the visibility of class members (attributes and methods).

Access Modifiers in Dart

Dart provides three access modifiers that allow you to control the visibility of class members:

  • Public: Members with no access modifier are considered public and can be accessed from any part of your code. Public members are denoted with no leading underscore.
  • Private: Members preceded by an underscore (_) are considered private and are accessible only within the same library. Private members should not be accessed directly from outside the library.
  • Protected: Dart doesn’t have a specific access modifier for protected members. Conventionally, a leading underscore is used to indicate that a member is protected. However, this is not enforced by the language and relies on developers adhering to the convention.
Example of Encapsulation

Let’s consider an example of encapsulation in Dart to better understand how access modifiers work. Here’s a class with both public and private members:


class Student {
    String name;          // Public member
    int _studentID;       // Private member

    Student(this.name, this._studentID);

    String get studentInfo {
        return 'Name: $name, ID: $_studentID';
    }
}
    

In this example, the name member is public, while the _studentID member is private due to the leading underscore. The studentInfo getter method is used to access the private member indirectly.

Benefits of Encapsulation

Encapsulation provides several benefits in Dart programming:

  • Data Protection: Private members are not directly accessible from outside the class, reducing the risk of unintentional data modification.
  • Code Organization: Encapsulation encourages well-structured and organized code by bundling related data and methods within a class.
  • Code Modularity: Encapsulation supports code modularity, making it easier to maintain and extend your code over time.
Getters and Setters

Dart allows you to define getters and setters to control access to private members. Getters provide read-only access to private members, while setters enable controlled modification. By using getters and setters, you can enforce validation or perform actions when accessing or modifying data.

Here’s an example of using getters and setters in Dart:


class Circle {
    double _radius;

    Circle(this._radius);

    double get radius => _radius;

    set radius(double value) {
        if (value >= 0) {
            _radius = value;
        }
    }
}
    

In this example, the Circle class uses a private member _radius and defines a getter and a setter for it. The setter enforces a non-negative value for the radius.

Conclusion

Encapsulation is a crucial concept in Dart that ensures data protection, code organization, and modularity. By controlling access to class members through access modifiers, you can design code that is more secure, maintainable, and extensible. Getters and setters further enhance the control and validation of data access and modification, making encapsulation a key feature in Dart programming.