In object-oriented programming, special types of methods are used to retrieve and set the values of an object by the user. We call the methods used to retrieve data getters (or accessors) and the methods used to set the values setters (or mutators).

By convention, their method names begin with the word “get” or “set”, respectively. Their premise and implementation are super simple (try not to complicate too much). See the example below in Java:

public class Person {
	private float age;
 
	public float getAge() {
		return age;
	}
	public void setAge(float value) {
		this.age = value;
	}
}

The benefit of getters and setters are that they control how attributes are accessed and updated in the code, and restricts behaviour accordingly.