- Why do we exactly need to use the set and get methods in our class that use private attributes?
- When it is really used during our program process?
- Can we still make it work without it (without changing the private attributes to public)?
This is an example of a source code when we are using the set and get methods:
public class Dog {
private String dogName;
private int dogAge;
public Dog(String dogName, int dogAge) {
this.dogName = dogName;
this.dogAge = dogAge;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogAge() {
return dogAge;
}
public void setDogAge(int dogAge) {
this.dogAge = dogAge;
}
@Override
public String toString() {
return "Dog{" + "dogName=" + dogName + ", dogAge=" + dogAge + '}';
}
}