Java OCA OCP Practice Question 2387

Question

Which option defines a well-encapsulated class?

a  class MyClass { 
       public String font; 
   } // ww  w .j  a va2 s .  co  m

b  class MyClass2 { 
       public String font; 
       public void setFont(String font) { 
           this.font = font; 
       } 
       public String getFont() { 
           return font; 
       } 
   } 

c  class MyClass3 { 
       private String font; 
       public String author; 
       public void setFont(String font) { 
           this.font = font; 
       } 
       public String getFont() { 
           return font; 
       } 
       public void setAuthor (String author) { 
           this.author = author; 
       } 
       public String getAuthor () { 
           return author; 
       } 

   } 

d  None of the above 


d

Note

Options (a), (b), and (c) are incorrect because they all define a public instance variable.

A well-encapsulated class should be like a capsule, hiding its instance variables from the outside world.

The only way you should access and modify instance variables is through the public methods of a class to ensure that the outside world can access only the variables the class allows it to.

By defining methods to assign values to its instance variables, a class can control the range of values that can be assigned to them.




PreviousNext

Related