Java OCA OCP Practice Question 1170

Question

Given the following class, what should be inserted into the two blanks to ensure the class data is properly encapsulated?

package mypkg; // w  w w.  j a  va2  s.  c o m
public class Main { 
        public String stuff; 
        ___   String ___() { 
           return stuff; 
        } 
     ? 
        public void setStuff(String stuff) { 
           this.stuff = stuff; 
        } 
} 
A. public and getStuff 
B. private and isStuff 
C. public and setStuff 
D. None of the above 


D.

Note

The class data, stuff, is declared public, allowing any class to modify the stuff variable and making the implementation inherently unsafe for encapsulation.

There are no values that can be placed in the two blanks to ensure the class properly encapsulates its data, making Option D correct.

If stuff was declared private, Options A, B, and C would all be correct.

Encapsulation does not require JavaBean syntax, just that the internal attributes are protected from outside access, which all of these sets of values do achieve.




PreviousNext

Related