Could someone explain what a getter and setter does in really simple words? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Could someone explain what a getter and setter does in really simple words?

8th May 2017, 3:21 PM
Tina
2 Answers
+ 6
Its like an engineer, set a value for an instance, or get it. For example (in real life) You make an empty book, you wrote a value: "Hello World" You setted it, now you are reading it, you're simply getting it. Haha get it? No? Set it first! Ba dum tss! :D
8th May 2017, 3:26 PM
Complex
Complex - avatar
+ 5
Getter and setter provide an interface to attributes of object... providing ability to do some treatments when getting or assigning a value: by just defining an attribut to an object, you can set any value to it, even if that will provocate an later error, by using getter and setter function, you let them filter the value and correct it or forgot it silently, preventing an later error. Imagine you have an object with an attribute storing a list index: with a setter, you can verify that the index you will store is valid ( not out of list boundary ).
9th May 2017, 4:19 AM
visph
visph - avatar