"If the facts don't fit the theory, get new facts" --Albert Einstein
Hunter McMillen wrote:Have a look at this page from the Java tutorials about visibility and access modifiers: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
The answer you are looking for it near the bottom
Hunter
Perry Terrance wrote:
My InnerClass is Protected - therefore it should be able to accessed by the subclasses of OuterClass even if it in another package (com.mypackage2).
Perry Terrance wrote:Ah - this is fascinating!
So what I'm guessing is happening is this:
By specifying the innerClass as Protected, the Default Constructor for innerClass is automatically set to Protected as well.
At the OuterClass member level, the Protected innerClass itself CAN be accessed by outerClass-subclasses in BOTH com.mypackage1 and com.mypackage2.
However, the Protected Constructor of innerClass itself is one-single layer below, so it can only be access by classes in the SAME com.mypackage1, NOT by com.mypackage2.
Did I get that right?
the Protected Constructor of innerClass itself is one-single layer below, so it can only be access by classes in the SAME com.mypackage1,
Perry Terrance wrote:
and to subclass innerClass - you will have to create a new class that extends outerClass and then within it create another "inner class" that extends innerClass.
My brain is melting...
Perry Terrance wrote:Also - I wonder about this - shouldn't this work as well?
Since AnotherInnerClass is a subclass of innerClass now, the Protected Constructor of innerClass should be accessible for all codes inside AnotherInnerClass right? However, in my Eclipse it keeps giving me a red-mark saying the innerClass constructor is not visible (which doesn't make sense....)
Jeff Verdegan wrote:
I think you'd get the same thing even if these weren't inner classes. Roughly speaking, the "accessible from subclass" bit of the protected access rule also includes "...but only through this. or super.".
Perry Terrance wrote:
Jeff Verdegan wrote:
I think you'd get the same thing even if these weren't inner classes. Roughly speaking, the "accessible from subclass" bit of the protected access rule also includes "...but only through this. or super.".
Interesting - this is first time I ever heard of this unique constraint of the Java language.
And frankly - I find it kind of odd this constraint doesn't happen when all the code in the same package - ergo:
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |