Answer for: I don't understand accessor methods
here is the thing:
why do we need accessor methods? Encapsulation !!!
Why do we need encapsulation?
1) Imagine you (programmer#1) gonna write those setAge, getAge and getName methods.
I am programmer#2. I most probably cant access age and name directly. So I have yo use your public accessor methods setAge, getAge and getName. This is to save your code and variables from mess. Cuz u cant trust all coders.
2) In setAge method u can provide VALIDATION
random evil programmer: ya I wanna make age equal to 234 so ur program results gonna crush hahaha
u: nah I gonna add validation condition into my setAge method so u can only make age equal from 0 to 90(whatever u want)
This is the most popular reason why we use accessor methods.
Code explanation:
setAge explanation( this is just to get main idea)
public void setAge(int ageInput) {
if ((ageInput > 10) && (ageInput <90))
{age = a;}}
Random evil programmer sends ageInput into your public method.
First of all, it checks if age value is correct. If yes, age instance(object) variable will become ageInput. If no, nothing will happen and your variables wont be messed up.
GetAge: it just returns current age value. Nothing fancy.
let me know if you have more questions ;) Best of luck to you.