File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Protected access from subclass in different package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Protected access from subclass in different package" Watch "Protected access from subclass in different package" New topic
Author

Protected access from subclass in different package

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I understand that if a subclass is in a different package than its super it can't directly access any protected members, but instead gets its own inherited copy to deal with. The same should go for inner classes. However, the following code fails, and I can't seem to determine why. The commented out section of course would give a compiler error, it's p2 that I don't understand why it is giving an error also.

Any ideas?
Jason
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ok, I added a public no-argument constructor that does nothing to my inner class, and it compiles fine. Why? What is it about inner classes and constructors that made this fail? Does the default constructor get created with protected access also and therefore only accessible by subclasses of the inner class, or classes in the same package?
Jason
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
A default constructor has the same access level as the class it's associated with. The JLS describes this here.


"I'm not back." - Bill Harding, Twister
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Thanks Jim!
Always just use public or private for the constructors, so entering new territory. Never sat down and read through the JLS, I think I know what I am doing this weekend
Jason
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sounds like a long weekend. I've never read it all the way through - I just refer to it whenever I need an authoritative answer on something. Enjoy...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Protected access from subclass in different package
 
Similar Threads
protected member subclassing
mock test question on protected access specifier
Please explain this code
protected access of class members
Package Doubt :(