Lookup "encapsulation" and "information hiding".
Basically, a class manages data and provides operations on them/ give access to them, but the internal representation of that data should kept private to the class. Like this, a class can change its internal representation without breaking other code.
E.g. consider:
A)
public Date date;
B)
private Date date;
public Date getDate(){...}
public setDate(Date date){...}
Later you choose to use Calendar instead of Date.
In case A) you cannot change the data type without breaking code.
In case B) you can do:
private Calendar calendar;
public Date getDate(){...}
public setDate(Date date){...}
public Calendar getCalendar (){...}
public setCalendar(Calendar calendar){...}
You can provide a conversion in the get/setDate methods. Client code doesn't break.
Another reason you sometimes want to use getter and setter is the use of libraries/
frameworks which base on JavaBeans patterns.