Encapsulation w/ getters and setters Flashcards

1
Q

What are the 3 benefits of encapsulation?

A

Hides implementation details
Simplifies maintenance
Controls client access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A getter is also called a….?

A

accessor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A setter is also called a….?

A

mutator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are setters used to do?

A

Set or update an existing variable’s value, especially when the instance data is defined as private inside a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the naming convention for getters?

A

getters begin with “get” followed by the name of the field using camel case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the naming convention for setters?

A

getters begin with “set” followed by the name of the field using camel case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Do you need to know how the getter method works and what’s “under the hood”?

A

No, the getter simply returns the value of the private attribute

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What flexibility does encapsulation give that can help deal with problems like the “Year 2000” (Y2K)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the return type of a setter?

A

Void because nothing is returned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the return type of a getter?

A

Whatever the data type of the field is

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What arguments do a getter method use?

A

None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What arguments do a setter method use?

A

A single argument of the same type are the data field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the “this.” in this.year = year; allow us to do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly