Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about protected access

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I understand that protected access is for inheritance even for subclass outside of superclass package.
so my question is, do you declare protected access in superclass's data or method and to access it in the subclass outside of the superclass package

you need to have a reference of the superclass, with importing superclass's package , to access data

or

you should extend the superclass, that way you inherit the data and the method.

Thanks for answering my question, hope it is clear what I am asking
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be able to access protected members of a class A, another class must a) be in the same package, or b) be a direct or indirect subclass of A. If a class B is not in the same package, simply having a reference is not enough; class B must extend class A (or a subclass of class A).
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thanks Rob for reply

Second, this sounds to me like how I am using Struts 2's ActionSupport class, since I don't have my action class in the same package as the ActionSupport class (com.opensymphony.xwork2.ActionSupport). But I extend it and I get to use methods like execute(), validate() and so on.

I am getting the concept right?

thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. Those methods are public, not protected. Therefore, you can call these methods through a reference from any class. The protected static Log LOG however, can only be called from your code if you subclass ActionSupport.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The protected static Log LOG however, can only be called from your code if you subclass ActionSupport.


Are you saying the subclass ActionSupport needs to be in the same package (com.opensymphony.xwork2.ActionSupport)

or just any subclass extends ActionSupport can use LOG?

thanks again
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any subclass can use LOG.

Access right are "incremental" - visibility is only added to. So if a class has visibility to a member, broadening the access will not remove that visibilty.
The complete overview:
private: only the class itself and its members have access
default (no access modifier): private, plus classes from the same package
protected: default, plus all direct and indirect subclasses
public: protected, plus any other class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic