Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whizlab question

 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In whizlabs demo tests one of the que goes
In java a "protected" variable is more accesible than a default variable (variable with no access modifier)
true
false
I chose true still it gave me wrong Seems like cant trust these commercial packages.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yogesh,
You probably just misread the problem.
Default member variables will be accessible from all classes in the same package and all subclasses. Protected variables will be accessibly only from subclasses. So, protected variables are less accessible than default variables, not more accessible--thus the false answer.
 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think "protected" can be accessed from
1.all members in the same package
2.or by subclasses of the class in which it is declared
and "default" can be accesses from
1.all members in the same package.
So protected wins by one point
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yogesh,
Protected members can ONLY be accessible from subclasses. You can check out the language specification to see for yourself.
To correct my last post: default members will be accessible from any class in the same package, but no subclasses defined outside the package in question. Protected members are still, as in my last post, only accessible to the current class and its subclasses.
 
Ed Mandera
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathaniel,
I thought the way this worked is that protected members can be accessed in all cases where default members can be accessed (when the accessing member belongs to the same package) AND via a subclass through inheritance.
From the language specification, a few lines above where you linked:
... if the member or constructor is declared protected, then access is permitted only when one of the following is true:
- Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.
- Access is correct as described in �6.6.2.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed,
You're right -- I'm wrong. I'm suddenly amazed I passed the SCJP exam at all. So, the original question should be answered true, and the Whizlab is incorrect. It took me all of 30 seconds with NetBeans to convince myself. Now to delete my posts and pretend this never happened!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic