Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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: 193
26
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: 10763
148
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: 193
26
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
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!