A Simple Guide to Getters and Setters in Object-Oriented Programming

I’ve had a lot of students get hung up on getters and setters when they first start implementing object-oriented programming principles–especially in C#, because its auto-properties can feel a little too much like magic.  In the simplest terms, getters and setters are methods used to access and manipulate the values of class fields and properties. In this article, we’ll take a look at getters and setters, and discuss when and how to use them.

What are getters and setters? 

Getters and setters are methods.  Depending on the language you’re using, they may not have the same syntax as other methods, but really, that’s all they are.  Getters, true to their name, ‘get’ the value of a property–that is to say, a getter returns the value of a class property.  A setter, also true to its name, ‘sets’ the value of a property.

private string _name; 

public string Name { 
	get {return _name;}
	set {_name = value;} 

In the example above (written in C#), our class has a private field, _name, and a public property, Name.  The _name field is private, meaning that it cannot be accessed outside of this class.  The Name property, whoever, is public.  The getter for the Name property returns the value of the private _name field, and the setter for the Name property sets the value of the private _name field.  The value keyword used here is a parameter–it represents the new value being passed in for assignment. 

In C#, getters and setters do not look much like methods, especially if you are using auto-property syntax.  When you use the auto-property syntax, it’s like a shortcut for having a private field and public property, with a public getter that returns the value of that property, and a public setter that assigns the value of that property.  Using auto-property syntax, we can collapse our example above down into one line of code: 

public string Name {get; set;} 

Can I see it in another language? 

To drive home the point that getters and setters are just methods, here’s what getters and setters look like in Java, using our “name” example: 

private String name; 

public String getName() { 
     return name; 

public void setName(String value) { 
     this.name = value; 

As you can see, the syntax for getters and setters in Java is the same as syntax for other methods!  The word ‘value’ is not a keyword in Java; I used it as the parameter for setName() to drive home the fact that the C# code in the previous example and Java code written above implement the exact same functionality.

Why are getters and setters useful? Why have getters and setters at all? 

These methods are used to ensure that the values of a class property can only be accessed or modified by the getter and setter methods.  This encapsulates the values and allows for an important level of abstraction–as the programmer, you decide how the values are accessed and modified, which allows the implementation of a class to be changed without affecting any code that uses it.  Getters and setters prevent other code from accidentally (or intentionally!) modifying values in a way that could cause errors, security vulnerabilities, or other harm  For example, if you created a class to represent a bank account, retrieving the account number may look something like this: 

private int _accountNumber; 

public string AccountNumber
	get { 
	     string accountString = _accountNumber.ToString();
             string lastDigits =  
                  accountString.Substring(accountString.Length - 5); 
             return $”xxxxxxxx{lastDigits}”;

…notice that the actual value of the bank account number is still available for use in the class through the private field, but the public getter method limits the digits returned to the last four, and the publicly-available AccountNumber property is a string, not an integer.  There is also no setter, which means that the AccountNumber property is read-only; its value cannot be assigned.  Outside code accessing the AccountNumber property would retrieve a masked, stringified version of the actual account number held in the private field.

What is a derived property? 

A derived property is a property whose values are computed (or, derived) from other information (such as the value(s) of one or more other properties). 

To follow our bank account example, when we retrieve the available balance for the account by using the getter for the AvailableBalance property, it is computed by subtracting the amount of pending transactions from the account balance: 

public int AvailableBalance 
	get { return _balance - GetPendingTransactionsAmount(); 

This makes sense, because the amount of pending transactions can change at any time.  Generally, charges to your account do not clear right away, so it may take awhile for your actual balance to change.  If you went on a shopping spree and swiped your debit card several times earlier in the afternoon, the balance of your account may not change until a day or two later, but the bank is aware of those pending transactions–so when you check the banking app on your phone to make sure you can pay for dinner that night, you need to see the balance of money that is actually available for you to use once all of your shopping charges have been accounted for.

In Conclusion…

Getters and setters are an important part of object-oriented programming.  They allow you to control access to fields and properties in classes, and provide abstraction and encapsulation that is vital to writing maintainable and reliable code.  By using getters and setters, we can make sure that the values of class fields and properties are valid and consistent, and that the internal workings or implementation of a class can be changed without affecting code that uses that class. 

Add a Comment