Protected inner classes and inheritance



Apologies if I am missing something obvious, but here's the situation. I have
a top-level class with a protected inner class. I have a second-level class
in a different package that extends the top-level class. For example:

package somePackage;

public class TopLevel
{
protected class Inner
{
protected int intField = 0;

protected Inner()
{
// constructor
}

protected void innerMethod()
{
return;
}
}
}


package somePackage.otherPackage;

import somePackage.TopLevel;
import somePackage.TopLevel.Inner;

public class SecondLevel extends TopLevel
{
public SecondLevel()
{
// constructor
}

public void someMethod()
{
Inner inner = new Inner();

inner.intField = 1;

inner.innerMethod();
}
}


When I compile the second class, I get at least 4 errors:

The type somePackage.TopLevel.Inner is not visible
The constructor TopLevel.Inner() is not visible
The field TopLevel.Inner.intField is not visible
The method innerMethod() from the type TopLevel.Inner is not visible

I thought I read the Java docuementation clearly when is says

"The protected modifier specifies that the member can only be accessed within
its own package (as with package-private) and, in addition, by a subclass of
its class in another package."

and

"You can use the same modifiers for inner classes that you use for other
members of the outer class. For example, you can use the access specifiers --
private, public, and protected -- to restrict access to inner classes, just as
you do to other class members."

If I simply make TopLevel.Inner a public class, the errors go away. But I
don't really want to do that... Anything obvious??


thanks
scott
.



Relevant Pages

  • Simple way to restore object relationships after deserialization?
    ... I'm working with XML serialization of hierarchical objects such as the ... public class OuterClass ... public InnerClass Inner ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: URLClassLoader problem with inner classes
    ... And then I have a class B extends A with a lot of inner ... static initialize function cause B is designed as a singleton. ... package, but have loaded the classes through different classloaders, then they ... It'll fail at runtime rather than ...
    (comp.lang.java.programmer)
  • Re: "Private" nested classes
    ... Wouldn't making the inner constructor private solved the problem though? ... "Etienne Boucher" wrote: ... > outside the outter class. ... > public class Outter ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: "Must instantiate controlled types at library level." Why?
    ... > instantiate that package in the declarative part of a procedure, ... subprogram, including a library level subprogram, is never at library level. ... procedure Inner is ...
    (comp.lang.ada)
  • Need a new access modifier?
    ... I've read somewhere that inner class access to a "private" member of a nesting class causes it to be silently treated as "package-private" by the compiler, ... Making it blank and making the "package" keyword do double duty as the access modifier for package-private members sounds clean but isn't source-compatible with older code at all -- package-private members suddenly become effectively private, in fact, in a lot of old code if that gets done. ... Maybe making the class loader reject a class that belongs to a package where a) another class that belongs to that package has the same name as the package's name's last dot-separated part, but with the first letter capitalizaed and b) that class is in a jar and the class being loaded either isn't or is in a different jar. ...
    (comp.lang.java.programmer)