what are GETTER and SETTER used for? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

what are GETTER and SETTER used for?

So GETTER and SETTER we use when we declare private variebles,right?

12th Dec 2016, 10:56 PM
Nochains - avatar
2 Answers
+ 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
12th Dec 2016, 11:16 PM
Nahuel - avatar
+ 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
12th Dec 2016, 11:20 PM
Dago VF
Dago VF - avatar