Created
October 16, 2023 02:10
-
-
Save abdorll/42cd936c77ec01c9adfc1040264c5fbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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