File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access modifier 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 "Access modifier" Watch "Access modifier" New topic
Author

Access modifier

Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
A class design requires that a particular member variable must be accessible for direct access by any subclasses of this class, but otherwise not by classes which are not members of the same package.
What should be done to achieve this?
A.The variable should be marked public
B.The variable should be marked private
C.The variable should be marked protected
D.The variable should have no special access modifier
E.The variable should be marked private and an accessor method provided
I thought the answer is D but it is C. If the variable is marked protected it can be accessed by subclasses outside the package right.
Please clarify
Sandeep Potnis
Ranch Hand

Joined: Aug 18, 2000
Posts: 39
The answer is in the question.
Sandeep
Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
hi Sandeep
The question says the variable should not be accessed from other packages but if the variable is protected it can be acessed from other packages right??
I said C coz of u dont have any modifier it has friendly scope and can be accessed only in the same package.
Am i right??
Sandeep Potnis
Ranch Hand

Joined: Aug 18, 2000
Posts: 39
Sunita,
This is the question:
A class design requires that a particular member variable must be accessible for direct access by any subclasses of this class, but otherwise not by classes which are not members of the same package.
What should be done to achieve this?
This means,
If subclass in any package
Have direct access
Else (otherwise)
Package access.
This is also the definition of protected access.

hth
Sandeep
Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
Thanks Sandeep
 
Don't get me started about those stupid light bulbs.
 
subject: Access modifier