0

Why to declare the members of class as private, and to access, we go for the setters and getters, instead we can go for public.

Why to declare the members of class as private, and to access, we go for the setters and getters, instead we can go for public.

16th Nov 2021, 12:24 PM
swamy
swamy - avatar
3 Respostas
+ 4
Hello Swamy, Please use appropriate tags ... https://code.sololearn.com/W3uiji9X28C1/?ref=app
16th Nov 2021, 1:14 PM
Ipang
+ 3
Making a variable private "protects" its value from being changed by the other classes that uses it. Those other classes should only access behavior by calling methods(getters and setters) on the class, not by changing values of variables directly. This is called "data hiding". If any class can change a variable's value, then it is difficult to ensure that the value is valid. You need to protect your data from being changed.
16th Nov 2021, 12:56 PM
Blen
Blen - avatar