+ 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??
1 Odpowiedź
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.