File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected access from subclass in different package

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A default constructor has the same access level as the class it's associated with. The JLS describes this here.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic