Complete the code to declare a private variable in a class.
public class Person { private String [1]; }
The variable name is declared private to hide it from outside access, which is a key part of encapsulation.
Complete the code to create a public getter method for the private variable.
public String [1]() { return name; }
The getter method is named getName to provide controlled access to the private variable name.
Fix the error in the setter method to properly set the private variable.
public void setName(String [1]) { this.name = [1]; }
The parameter name should match the variable name to correctly assign the value using this.name = name;.
Fill both blanks to complete the encapsulated class with private variable and public getter.
public class [1] { private int [2]; public int getAge() { return age; } }
The class is named Student and the private variable is age to demonstrate encapsulation.
Fill all three blanks to complete the class with private variable, getter, and setter methods.
public class [1] { private String [2]; public String getName() { return [2]; } public void setName(String [3]) { this.name = [3]; } }
The class is Employee, the private variable is name, and the setter parameter is also name to properly encapsulate the data.