3

I'm doing an assignment which asks us to define a class for a rectangle and equip it with various methods, two of which is getHeight() and getWidth(), which aren't supposed to do anything other than return this.height; and return this.width;. I don't understand the point. If I ever want to access the width or height, why wouldn't I just do it by referencing this.width instead of this.getWidth()?

Micha
  • 5,117
  • 8
  • 34
  • 47
Benjamin Lindqvist
  • 4,300
  • 3
  • 18
  • 19
  • As only one example, perhaps you don't want to expose `width` or `height` because you don't want them to be directly settable. – apsillers Jul 01 '13 at 20:03
  • 3
    see encapsulation http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) – erkanyildiz Jul 01 '13 at 20:04
  • 1
    http://stackoverflow.com/questions/1568091/why-use-getters-and-setters or http://stackoverflow.com/questions/10407877/what-is-the-point-of-getters-and-setters – projeqht Jul 01 '13 at 20:04
  • 1
    http://programmers.stackexchange.com/questions/21802/when-are-getters-and-setters-justified – apsillers Jul 01 '13 at 20:05
  • [This](http://stackoverflow.com/questions/4156820/when-to-use-get-set-methods-in-java) is specific for Java, but it provides some universal reasons. – ngwilliams Jul 01 '13 at 20:05

3 Answers3

10

The main point is: To separate the interface from the implementation.

The attribute getter and setter functions (getWidth() and getHeight()) are the interface. The actual variables this.width and this.height are the implementation.

Only the interface matters to the outside world, not the implementation. The implementation should stay internal (data encapsulation).

Having setters and getters sometimes feels like a typing exercise when writing the rudiments of a class, but it usually pays off when the class and its interface gets more complex. Some IDEs like Eclipse for Java and C++ support generating the boilerplate code for new data members.

For very simple classes the separation seems pointless and one sometimes omits the getters and setters and grants direct access. One example would be a Point2d class with members x and y.

But as soon as the object becomes more complex getters and setters are a good idea. You can later change the implementation without changing the interface. You can for example later notify other instances when the width of your object changes.

One major application for getters is to implement a read-only interface to some aspects: In this case there is just a getter method, no setter, and the data elements are made private. This way you prevent code with uses the class from accidentally changing the value.

Some languages support marking parts of the class as private or internal to the implementation, either by position or by marking them with special keywords like private.

Other languages like Python do not explicitly support this strict data encapsulation, but it can still be achieved by prefixing internal data members with a certain prefix or suffix by convention (double or single underscore in Python, double underscore even mangles the name somewhat).

Johannes Overmann
  • 4,914
  • 22
  • 38
5

This is called information hiding, and there are a variety of reasons to utilize it. For instance, you generally don't want to give an outside user access to this.height or this.width because you wouldn't want him or her to alter them directly (perhaps setting them to invalid values?). In general, you want to hide how data is stored in your class, and only allow access via methods you yourself define.

Of course, sometimes you don't need to worry about this; if your fields are declared as final for example, then it might in some cases be alright to grant direct access. This can be seen with all arrays, which have a public final int field called length. What makes direct access to a final field more appropriate is knowing that a user can never alter it.

Moreover, the use of getters and setters makes your code easier to maintain and update in the future. What if, for instance, you wanted to change how the height is reported in the future? i.e. report 0 if it's below a certain threshold. This wouldn't be possible if people were already accessing the height field directly.

arshajii
  • 127,459
  • 24
  • 238
  • 287
4

The basic principle around this, is the separation between the implementation and the use of the class (as defined by the class interface). If you force the class user to access the class attribute via "setter" and "getter" methods, you will later be able to change the way those attributes are implemented, without changing the interface. For example, the attributes may be calculated instead of stored, or they may be stored in a database instead of memory variables, etc, and the user of the class doesn't need to know or care.

theglauber
  • 28,367
  • 7
  • 29
  • 47