This week's book giveaway is in the Cloud forum.
We're giving away four copies of The Business Blockchain and have William Mougayar on-line!
See this thread for details.
Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Review Questions #6 in chapter 4, on page 219 (Java OCA 8 Programmer I Study Guide)

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand this question.



I chose option B, because I understand such as "is not visible" belong to private fields and package private methods. Then I saw correct answer I thought that I understand wrong, "is not visible" belong to public class, not private fields and package private methods. The correct answer is D, but I can't understand it. I read that we can use access modifiers for all methods and instance variables. Maybe my english is not enough for understanding option D. Please explain it. Thanks in advance
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us walk through every possible answer. First the wrong ones:
A. private (most restrictive) < default / package-private < protected < public (least restrictive)
B. if the class is public, the class is visible to any class (in or outside the package) (the access level of fields and methods is not relevant to determine if the class itself is visible)
C. Such access modifier doesn't exist (you only have 3 access modifiers and 4 access levels). So any package-private class is visible to any class in that package, no possibility to restrict this
E. That's complete nonsense: there is no access modifier which restricts access based on the name of a class, method or variable

Now the correct one. Answer D is nothing more than thisI used access modifiers to allow access to all methods (using the public access modifier) and using the private access modifier I made sure you can't access any instance variable.

Hope it helps!
Kind regards,
Roel

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: Now the correct one. Answer D is nothing more than this
I used access modifiers to allow access to all methods (using the public access modifier) and using the private access modifier I made sure you can't access any instance variable.


After your example everthing is clear, Roel. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic