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

Q of inner class in different package

 
Claire Yang
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following codes to test the access modifiers of inner classes:

When compiled, line1 is ok, but line2 causes a compile error: Inner2() has protected access in p1.A.Inner2(both the code--remove package&import lines--are ok if they were put in the same directory). Where am I wrong? Can we use inner class as general class members? Or using inner class like this is meaningless?
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well member inner classes are like other class member variables, methods, thus to access a protected member from a subclass of another package, you have to access it through an instance of the subclass not the super class.
Thus if you change your code to the following, it should work just fine.

HTH
 
Claire Yang
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, Alfred.
I modified the code as you said, I still got the same compile error:
I compiled the 2 files in their parent directory with the following commands:
javac p1\A.java
javac p2\A.java
I think it's right.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic