• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarification in question 6 of chapter 4(page 219) in Java OCA 8 Programmer I Study Guide, Sybex

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the question, it says option B. A public class that has private fields and package private methods is not visible to classes outside the package

and in the answer it says Option B is incorrect because the class is public. This means that other classes can see the class. However, they cannot call any of the methods or read any of the fields. It is essentially a useless class.

don't these 2 statements in bold have the same meaning, that you can instantiate or create a reference variable, but cannot access their private, default fields .then option B must be true right.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create a reference variable , provided you import the class.
 
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
Ramya Subraamanian wrote:don't these 2 statements in bold have the same meaning, that you can instantiate or create a reference variable, but cannot access their private, default fields .then option B must be true right.

No, option B is definitely false! And thus the study guide is spot-on. A public class is visible/accessible by the whole Java universe and not restricted to only classes in the same package (as option B suggests).
The access modifiers of the members (fields and methods) don't have any influence on the visibility/accessibility of a class. These are just added by the authors to confuse you. And it has worked

Just remember: a public class is visible/accessible everywhere, a default class (no access modifier) is only visible/accessible by classes in the same package.

Hope it helps!
Kind regards,
Roel
 
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
Ramya Subraamanian wrote:you can create a reference variable , provided you import the class.

Again not true! Using an import statement is not required at all. If you use the fully qualified name of a class, you don't need an import statement at all.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you, but i felt option B is just talking about the accessibility of the private fields and package private methods being not visible and not about the class being public itself...but okay ..i am sort of convinced
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:but i felt option B is just talking about the accessibility of the private fields and package private methods being not visible and not about the class being public itself

Let's try to completely convince you Check the verb of option B. It's "is", because a public class is singular and thus you use "is". If it was about instance fields and methods, it would have been plural and thus the verb "are" would have been used instead.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..right. I get it. You are good at convincing .... ..but if I get a question like this in the exam , I would instinctively choose option b to be true, that's what scares me
 
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
Ramya Subraamanian wrote:but if I get a question like this in the exam , I would instinctively choose option b to be true, that's what scares me

On the actual exam each question will mention how many correct answers to select. So if you have gone through all answers, you'll probably have more "correct" answers than you can select for this question; meaning you have at least one wrong answer selected and should evaluate your selected answers again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic