In classes, variables are often made private for encapsulation, and to limit the variables to a certain scope allow better error control and fewer bugs. This makes sense, as the fewer places a variable can be accessed the fewer places a bug can occur with that variable.
However, I always see variables made private, and then a getter and setter function used to retrieve that value (sometimes even a pointer to that variable!). For example int a
is private to prevent public access, but then getA()
and setA()
allow direct access to them.
So don't getter functions and setter functions defy the point of it being private? I mean private variables with accessor function are the same as public variables, only the code to access them changes. (object.variable vs object.getVariable())
Is there a reason people make variables private with accessor functions? Are there any advantages when compared to making it public?
I am talking about programming in general, but mostly in C languages (i.e. C, C++, C#, Obj-C).
Because if you change the internal representation of that variable or want to do more when it's set or retrieved, it won't break all the other classes that use it (and if it's a library, you don't have to change your API).
It also means you can easily set a breakpoint to see when it gets used (although most languages/debuggers have data breakpoints of some sort).
Why was encapsulation required? Why was oop required? Wasnt C programming language capable of doing what we do today? Ask this to yourslef. Or if you have worked on huge systems having millions of lines of code. And all you used was public variables of a crucial data structure accessible from every module of the program.
I had the same question in mind when I started learning Object oriented programming because in most of the books they just make variable private and add public methods (Getter/Setters) to access them, So I was thinking if I can access that variable by methods that are public then what is point to make that variable private.
I get answer When I start implementing actual business Application.
Let's consider a class student which contain Student name, roll no, marks of 3 subjects
Here make private variables because of 2 reasons
Validation: if a user provides invalid value to marks then student object should not create with invalid data and throw appropriate error/Exception. In the case of marks as public varibles user can set any value to them , So I added validation/Security to ensure severy student object created is having correct data.
Security In the case of percentage, user cannot set his own percentage. percentage is calculated and set internally. Here I do not Provide setter method for the percentage so User can only get/read percentage value by getter method.
This leads to security in abstraction that is only limited(read only) access to class variable.
The key word and tag here is "encapsulation". You're hiding the details of
a
, while still making it usable. I like the reasons already listed, there are many more. Here's another, you're debugging, and you finda
has an incorrect value. Ifa
is public, you'd have to check every placea
is accessed. Ifa
is private with a setter method, you know the only placea
could have changed is in a call tosetA()
- would be a great place to put a breakpoint ;)Maybe you will want to add some checks to the next version of the library (or to do something when someone read the value), and if the variable will be public in the current version, upgrade the library version will cost a lot of work.
Class defines the behavior and the members are the state of the object...so having a setter and a getter defines the encapsulation behavior of the class of letting others find/change the objects state.
In other terms the difference is something like letting your neighbour come into your house and take what he wants(making all the object in class public)....or making sure that the neighbour comes asks me on what he wants and I give him(having a getter/setter....)