Dart Private and Public Members


Private and Public Members in Dart

In Dart, members (fields and methods) of a class can be either public or private. Dart does not have explicit keywords like public, private, or protected (as in some other languages like Java or C++). Instead, Dart uses a naming convention to specify access control:

  • Public members: By default, all members are public and can be accessed from anywhere in the code.
  • Private members: Any member that starts with an underscore (_) is considered private to the library (i.e., the Dart file). Private members cannot be accessed directly from outside the library they are defined in.

Key Points:

  • Public members can be accessed anywhere in the program.
  • Private members are only accessible within the same Dart file (same library). If you try to access a private member from outside its file, you will get an error.

Example of Private and Public Members in Dart

Here is an example of how public and private members work in Dart:

Example Code:

class Person { // Public field String name; // Private field int _age; // Constructor Person(this.name, this._age); // Public method void displayInfo() { print('Name: $name, Age: $_age'); } // Private method void _celebrateBirthday() { _age++; print('Happy Birthday, $name! You are now $_age years old.'); } // Public method to call private method void celebrateBirthday() { _celebrateBirthday(); } // Public getter for the private _age field int getAge() { return _age; } } void main() { // Creating an object of the Person class var person1 = Person('Alice', 30); // Accessing the public field print(person1.name); // Output: Alice // Calling the public method person1.displayInfo(); // Output: Name: Alice, Age: 30 // Accessing the private field directly will result in an error // print(person1._age); // Error: The getter '_age' isn't defined for the class 'Person'. // Calling the public method that accesses the private method person1.celebrateBirthday(); // Output: Happy Birthday, Alice! You are now 31 years old. // Accessing private member through a getter method print(person1.getAge()); // Output: 31 }

Explanation:

  1. Public Members:

    • String name; is a public field. It can be accessed directly from outside the class (in main()), such as person1.name.
    • void displayInfo() is a public method. It can also be called directly from outside the class, like person1.displayInfo().
  2. Private Members:

    • _age is a private field. It starts with an underscore (_), making it private to the library (i.e., the current Dart file). It cannot be accessed directly from outside the class (e.g., person1._age will result in an error).
    • _celebrateBirthday() is a private method. It also starts with an underscore and is only accessible inside the Person class.
  3. Accessing Private Members:

    • Private members like _age can be accessed inside the class using methods such as celebrateBirthday(), which internally calls the private _celebrateBirthday() method.
    • A getter method (getAge()) is provided to safely access the value of the private _age field from outside the class.

Output:

Alice Name: Alice, Age: 30 Happy Birthday, Alice! You are now 31 years old. 31

Summary of Public and Private Members:

  1. Public Members:

    • By default, members are public in Dart.
    • Public members can be accessed from anywhere, including outside the class.
    • Public fields and methods don’t have an underscore (_) prefix.
  2. Private Members:

    • Private members start with an underscore (_).
    • Private members are only accessible within the class or the library (Dart file) in which they are declared.
    • To access private members externally, we usually provide getter or setter methods.
    • Private methods cannot be directly called from outside the class.

Best Practices:

  • Use private members to encapsulate internal state and implementation details.
  • Expose necessary information and behavior through public methods or getter/setter methods.
  • Private members help in data hiding and protect sensitive data from external modifications.