GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q of inner class in different package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q of inner class in different package" Watch "Q of inner class in different package" New topic
Author

Q of inner class in different package

Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
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

Joined: Aug 14, 2002
Posts: 279
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


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
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.
 
GeeCON Prague 2014
 
subject: Q of inner class in different package