Which statement(s) about the following class would help to properly encapsulate the data in the class?
package mypkg; /*from w w w .java 2s .c o m*/ public class Protect { private String material; protected int strength; ? public int getStrength() { return strength; } public void setStrength(int strength) { this.strength = strength; } }
A.
The access modifier of strength is protected, meaning subclasses and classes within the same package can modify it.
Changing the value to private would improve encapsulation by making the Protect class the only one capable of directly modifying it.
The first statement is correct.
The second and third statements do not improve the encapsulation of the class.
While having getters and setters for private variables is helpful, they are not required.
Encapsulation is about protecting the data elements.
With this in mind, it is clear the material variable is already protected.
Option A is the correct answer.