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

Access modifier

 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is in the question.
Sandeep
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sandeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic