0
what are GETTER and SETTER used for?
So GETTER and SETTER we use when we declare private variebles,right?
2 Antworten
+ 3
Getter and Setter are just like any other function in a class.
One will return the value of a var, and the other will change it.
let's say, person class, where name its private.
Person p1 = new Person();
p1.setName("Nochains");
print(p1.getName()); //this will print Nochains
+ 1
One of the principles of OOP is encapsulation, in resume means that you can't access directly to variables in the one class from another class
So you declare your variables as private
And to access and change the values you declare public methods (getters and setters) only these have the capacity to read and change the values of your private variables