Re: Protected nested generic constrained inheritance
MRe wrote:
Two example programs below, I'm wondering if someone could tell me
why example 1 doesn't work, given that example 2 does? The error says
that a nested class has protected access, but I am accessing it from
an inheriting class
/////////////////////////////////////////////////////////////////////
// Example 1
/////////////////////////////////////////////////////////////////////
// test/A.java
package test;
import test.A.NA;
^^
This import accomplishes nothing.
public class A<T extends NA>
The use of type 'NA' here requires that 'NA' be public. You've exposed 'NA'
to view beyond same-package or inheriting types.
{
protected static class NA
{
}
}
// test/extend/B.java
package test.extend;
import test.A;
import test.extend.B.NB;
^^
This import accomplishes nothing.
public class B
extends A<NB>
'NB' has to extend 'NA', but you are declaring it "above" the inheriting
scope, so 'NA' is not visible.
{
// ERROR : test.A.NA has protected access in nestedtest.A
What *exactly* does the error message state, in its entirety? Copy and paste;
do not paraphrase or redact.
protected static class NB
extends A.NA
{
}
}
/////////////////////////////////////////////////////////////////////
// Example 2
/////////////////////////////////////////////////////////////////////
// test/A.java
package test;
public class A<T>
This declaration does not elevate any protected member to public view.
{
protected static class NA
{
}
}
// test/extend/B.java
package test.extend;
import test.A;
public class B
extends A<Void>
This declaration does not require a public view of any protected member.
{
// No error
protected static class NB
extends A.NA
This declaration is contained within the declaration scope of an inheriting
class and is therefore legal.
{
}
}
--
Lew