+ 1

What is the use of Get and Set?

I can't understand the difference between program with get,set and program without get set properties. They both give same output. https://code.sololearn.com/cgM4lQHth3rx/?ref=app https://code.sololearn.com/ccuZ4wu5sFD1/?ref=app

11th Jun 2020, 2:34 PM
Armaandeep Singh
3 odpowiedzi
+ 2
That's basically just a dummy for getters and setters that doesn't make a lot of sense as it is. The difference is this: If you can just write into the object directly, using a public attribute, there is no encapsulation. Everything in a program could change the state of the object. Setters and Getters are like guardians of the object. The attribute itself is private and can only be changed from the inside. Now a setter can control the incoming value and - for example - deny it if it's not valid for the object. And a getter can make sure that not the internal attribute itself is exposed to the outside, but - for example - only a copy or a representation. Like this, the object itself controls who can change it, and how, and under which circumstances.
11th Jun 2020, 3:59 PM
HonFu
HonFu - avatar
+ 3
Do you have an example of such a program where it's the same? We could use it to explain the difference or find a better example.
11th Jun 2020, 2:46 PM
HonFu
HonFu - avatar
+ 1
Thanks HonFu. I was confused whether there is a necessity of get set or not and you have fit clarity about them in my mind
13th Jun 2020, 2:16 PM
Armaandeep Singh