S
Scott Harper
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
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