Skip to content

Instantly share code, notes, and snippets.

@abdorll
Created October 16, 2023 02:10
Show Gist options
  • Save abdorll/42cd936c77ec01c9adfc1040264c5fbb to your computer and use it in GitHub Desktop.
Save abdorll/42cd936c77ec01c9adfc1040264c5fbb to your computer and use it in GitHub Desktop.
// WITHIN A SINGLE FILE, IT WON'T WORK
class Employee {
// Private property
var _name;
// Getter method to access private property _name
String getName() {
return _name;
}
// Setter method to update private property _name
void setName(String name) {
this._name = name;
}
}
void main() {
var employee = Employee();
// ENCAPSULATION WON'T WORK
employee._name = "John"; // It is working, but why?
print(employee.getName());
}
// ENCAPSULATION WORKS WHEN THE PROPERTY/METHOD OF THE CLASS BEING INVOKED IS
//IN A FILE DIFFERENT FROM THE FILE IT IS DECLARED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment