wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whizlab question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Whizlab question" Watch "Whizlab question" New topic
Author

Whizlab question

Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53
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.


When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53
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

Joined: May 29, 2003
Posts: 1258
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

Joined: Jan 27, 2004
Posts: 4
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

Joined: May 29, 2003
Posts: 1258
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!
 
 
subject: Whizlab question