Here's a simple example:
Imagine you have a class called Car. You can create objects of this type - instances of the Car class.
Now imagine the Car class has a property called "colour". Each Car object has its own copy of this property, so each car can have its own colour setting. Following best practice you would make the colour property private, so the code might look like this;
class Car {
private Color colour;
}
However, you wouldn't then be able to change the colour of a Car object because you can't access the colour property. To do that you need a setter method, which in this case, would be called setColour:
class Car {
private Color colour;
public void setColour(Color colour) {
this.colour = colour;
}
}
When you call this method on a Car object, it just sets the car's own colour to the one you're passing in:
aCar.setColour(Color.RED)
anotherCar.setColour(Color.GREEN);
Similarly you would have a getColour() method that returns the colour of the car. The setColour and getColour methods just provide read/write access to the colour property inside a Car object, because that property is private.
The reason for "hiding" the colour setting, only to then have to add two methods to get access to it again, is because in more complex classes there might be additional steps to perform when a property is changed or read. Or you might not have one of them (giving read-only or write-only access to the property).
Hope this helps.
Most simple exercises have a getter and setter that do nothing but return the value, and set it to the new value, respectively. This is fine for simple examples, but in the real world, what would be the point of that? Why not just make it public? You almost always have some reason for making it private, and the function of getters and setters follow from that. Maybe the class is immutable, so there's no setter. Maybe the value can't be negative, so you throw an exception or set a default if it is. That's something they usually glaze over in classes. I didn't understand the point of getters and setters until I was out of programming 101, because they didn't explain what the purpose of writing them was, just how to do it.