Java SE 8 Programmer II — Question 24
Given the definition of the Vehicle class:
class Vehicle {
String name;
void setName (String name) {
this.name = name;
}
String getName() {
return name;
}
}
Which action encapsulates the Vehicle class?
Vehicle class public.
Answer options
- A. Make the name variable public.
- B. Make the setName method public.
- C. Make the name variable private.
- D. Make the setName method private.
- E. Make the getName method private.
- F. Make the
Correct answer: B
Explanation
Encapsulation involves restricting access to certain components of a class. Making the setName method public allows controlled access to modify the private name variable, thus preserving encapsulation, while the other options either do not enhance encapsulation or limit functionality.