permaculture playing cards*
The moose likes Beginning Java and the fly likes Question about protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about protected access" Watch "Question about protected access" New topic
Author

Question about protected access

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
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

Joined: Oct 27, 2005
Posts: 19680
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
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

Joined: Oct 27, 2005
Posts: 19680
    
  18

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

Joined: Aug 05, 2007
Posts: 294
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

Joined: Oct 27, 2005
Posts: 19680
    
  18

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about protected access