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.
3 Answers
+ 4
Hello Swamy,
Please use appropriate tags ...
https://code.sololearn.com/W3uiji9X28C1/?ref=app
+ 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.