Dart: Encapsulation

Dart: Encapsulation

In Dart, Encapsulation means hiding data within a library, preventing it from outside factors.

It helps you control your program and prevent it from becoming too complicated.

Every .dart file is a library. A library is a collection of function and classes.


In Dart, encapsulation is achieved through access modifiers. Dart provides three levels of access control:

Private Members: Dart provides support for private members using an underscore (_) prefix before the member's name. Private members can only be accessed within the same library.

 class Example {
   String _privateVar = "I am private!";

   void _privateMethod() {
     print("This is a private method.");
   }
 }

Public (no modifier): Members (variables or methods) declared with no modifier are accessible from anywhere, both within and outside the class.

 class Example {
   String publicVar = "I am public!";

   void publicMethod() {
     print("This is a public method.");
   }
 }

Protected:

Dart doesn't have this one directly, but we can pretend by using an underscore prefix (e.g., _age). It's like a secret handshake for our "library" and its subclasses.


Getter & Setter Method:

Getter and setter methods are the class methods used to manipulate the data of the class fields.

Getter is used to read or get the data of the class field whereas setter is used to set the data of the class field to some variable.

Getter Method in Dart:

It is used to retrieve a particular class field and save it in a variable. All classes have a default getter method but it can be overridden explicitly.

The getter method can be defined using the get keyword as:

return_type get field_name{
    ...
}

It must be noted we have to define a return type but there is no need to define parameters in the above method.

Setter Method in Dart:

It is used to set the data inside a variable received from the getter method. All classes have a default setter method but it can be overridden explicitly.

The setter method can be defined using the set keyword as:

set field_name{
    ...
}

Example:

class Person {
  String _name; // Private field
  int _age;    // Private field

  // Constructor
  Person(this._name, this._age);

  // Getter for _name
  String get name => _name;

  // Setter for _name
  set name(String newName) => _name = newName;

  // Setter for _age
  set age(int newAge) => _age = newAge;

  // Getter for _age
  int get age => _age;

  // Method to print the person's details
  void printDetails() {
    print('Name: $_name, Age: $_age');
  }
}
void main() {
  var person = Person('Jinali', 21);


  // Accessing private fields using getters
  print(person.name); // Output: Jinali
  print(person.age);  // Output: 21


  // Using setters to update private fields
  person.name = 'Reet';
  person.age = 14;


  person.printDetails(); // Output: Name: Reet, Age: 14
}

Why Encapsulation

  1. Security: Encapsulation keeps our data safe and prevents unauthorized access.

  2. Organization: It helps us keep our code clean and tidy, making it easier to understand and maintain.

  3. Control: We decide who can play with our data and how they can do it, just like a boss!