Encapsulation w/ getters and setters Flashcards
What are the 3 benefits of encapsulation?
Hides implementation details
Simplifies maintenance
Controls client access
A getter is also called a….?
accessor
A setter is also called a….?
mutator
What are setters used to do?
Set or update an existing variable’s value, especially when the instance data is defined as private inside a class
What is the naming convention for getters?
getters begin with “get” followed by the name of the field using camel case
What is the naming convention for setters?
getters begin with “set” followed by the name of the field using camel case
Do you need to know how the getter method works and what’s “under the hood”?
No, the getter simply returns the value of the private attribute
What flexibility does encapsulation give that can help deal with problems like the “Year 2000” (Y2K)?
Unlike other programing languages at the time with Java encapsulation the attribute names or data types may change. As long as the name of the getter methods stay the same, the program still works.
What is the return type of a setter?
Void because nothing is returned
What is the return type of a getter?
Whatever the data type of the field is
What arguments do a getter method use?
None
What arguments do a setter method use?
A single argument of the same type are the data field
What does the “this.” in this.year = year; allow us to do?
It allows us to use the same variable name in our setter method as the variable we are setting. This eliminates the need to come up with a different name for every argument.