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

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.