+ 1

Why we place setName() function in a public sector.. we can simply make variable public??

As setName() function is used to change the variable value but why we place this function in a public sector.. however we can simply make name variable public.. as we can use function in main we simply also can use the variable but why we make our variables private??

15th Jul 2019, 12:40 AM
Nouman Bin Sami
Nouman Bin Sami - avatar
1 Answer
0
for validation check. with public member the outside code are able to alter the variable without any check, and could lead to an error. but with setter, we can make sure that the value that going in is safe.
15th Jul 2019, 1:05 AM
Taste
Taste - avatar