What is the access point to private variables through the getter and setter (accessor) functions?

In classes, variables often become closed to encapsulation, and restricting variables to a specific area allows better control of errors and fewer errors. This makes sense, since the fewer places to access a variable, the less places an error can occur with this variable.

However, I always see variables made private, and then the getter and setter function used to extract this value (sometimes even a pointer to this variable!). For example, int a is private to prevent public access, but then getA() and setA() allow direct access to them.

So getter functions and setter functions do not ignore its quotient? I mean, private variables with an access 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 private variables with access functions? Are there any advantages over making it publicly available?

I talk about programming in general, but mostly in C languages ​​(e.g. C, C ++, C #, Obj-C).

+8
object oop class encapsulation
source share
6 answers

The keyword and tag here is "encapsulation." You hide details of a while retaining its use. I like the reasons already listed, there are many more. Here the other one, you are debugging, and you will find that a has the wrong value. If a is publicly available, you will need to check every place that a refers to. If a is private with the setter method, you know that the only place a could be changed was in the setA() call - that would be a great place to place a breakpoint;)

+11
source share

Because if you change the internal representation of this variable or want to do more when it is set or retrieved, it will not break all other classes that use it (and if it is a library, you do not need to change your API).

It also means that you can easily set a breakpoint to see when it will be used (although most languages ​​/ debuggers have some data breakpoints).

+2
source share

You might want to add some checks to the next version of the library (or do something when someone reads the value), and if the variable is publicly available in the current version, updating the library version will be expensive.

+2
source share

The class determines the behavior, and the members determine the state of the object ... therefore, the presence of a setter and getter determines the encapsulation behavior of the class, allowing others to find / change the state of objects.

In other words, the difference is to allow your neighbor to enter your house and accept what he wants (making all the objects in the class publicly accessible) .... or make sure that the neighbor asks me what he wants and I give him (having getter / setter ....)

0
source share

Why is encapsulation required? Why was this required? Was there a C programming language capable of doing what we do today? Ask about it. Or if you were working on huge systems with millions of lines of code. And all that you used are public variables of an important data structure, accessible from each program module.

0
source share

I had the same question when I started studying object-oriented programming, because in most books they just make the variable private and add public methods (Getter / Setters) to access them. So I was wondering if I could access this variable using methods that are publicly available, something that indicates that this variable has been closed.

I get an answer When I start to implement the actual business application.

Consider a student class that contains the student’s name, roll no, 3-item tags

 Class Student { private int rollno; private int java_marks; private int cpp_marks; private int unix_marks; private int percentage; public int getJava_marks() { return java_marks; } public void setJava_marks(int java_marks) { if (java_marks > 100) { System.out.println("Marks value should be less than 100"); //throw exception } this.java_marks = java_marks; } public int getCpp_marks() { return cpp_marks; } public void setCpp_marks(int cpp_marks) { if (cpp_marks > 100) { System.out.println("Marks value should be less than 100"); //throw exception } this.cpp_marks = cpp_marks; } public int getUnix_marks() { return unix_marks; } public void setUnix_marks(int unix_marks) { if (unix_marks > 100) { System.out.println("Marks value should be less than 100"); //throw exception } this.unix_marks = unix_marks; } public int getPercentage() { this.percentage = (this.cpp_marks + this.unix_marks + this.java_marks) /3; return percentage; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } } 

Private variables are created here for 2 reasons

  • Validation: if the user provides an invalid value for labels, then the student object should not create invalid data and cause the corresponding error / exception. In the case of signs as public variables, the user can set a value for them. So I added validation / security to ensure that the created student object had the correct data.

  • Security In the case of interest, the user cannot set his own interest. The percentage is calculated and set domestically. Here I do not provide the setter method for percent, so the user can get or read the percentage value with the getter method.

This leads to security in the abstraction, which restricts access to the class variable (read-only).

0
source share

All Articles