From: MRe on 4 Feb 2010 13:10 Hi, 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 Thank you, Kind regards, Eliott ///////////////////////////////////////////////////////////////////// // Example 1 ///////////////////////////////////////////////////////////////////// // test/A.java package test; import test.A.NA; public class A<T extends NA> { protected static class NA { } } // test/extend/B.java package test.extend; import test.A; import test.extend.B.NB; public class B extends A<NB> { // ERROR : test.A.NA has protected access in nestedtest.A protected static class NB extends A.NA { } } ///////////////////////////////////////////////////////////////////// // Example 2 ///////////////////////////////////////////////////////////////////// // test/A.java package test; public class A<T> { protected static class NA { } } // test/extend/B.java package test.extend; import test.A; public class B extends A<Void> { // No error protected static class NB extends A.NA { } }
From: Lew on 4 Feb 2010 14:47 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
From: markspace on 4 Feb 2010 17:13 MRe wrote: > The error says > that a nested class has protected access, but I am accessing it from ^^^^^^^^^ > an inheriting class ^^^^^^^^^^ Now go read up on what protected actually does (hint: I used Google to find this, it was the first link): <http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html>
From: MRe on 4 Feb 2010 19:27 Hi Lew, Thank you for the response.. > > import test.A.NA; > > ^^ > This import accomplishes nothing. That's what I thought, but NetBeans6.8 seems to insist > > 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. Oh, well that makes sense. I guess I missed it because Java didn't complain. I see now that it will compile in the absence of B so long as I keep every thing in the test package. I was mixing up several different things; sorry. Thank you :) > > // 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. That was the whole error. > > public class A<T> > > This declaration does not elevate any protected member to public view. > > > 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. Understood. Thank you > Lew Thanks again, Kind regards, Eliott
From: MRe on 4 Feb 2010 19:42 On Feb 4, 10:13 pm, markspace <nos...(a)nowhere.com> wrote: > MRe wrote: > > The error says > > that a nested class has protected access, but I am accessing it from > ^^^^^^^^^ > an inheriting class > ^^^^^^^^^^ > > Now go read up on what protected actually does (hint: I used Google to > find this, it was the first link): > > <http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html> Hi markspace, Thank you for the response and interesting link. I'm not sure I understand the mistake I've made in the quote you've highlighted (given the context in which you've quoted it) - as far as I can tell, the linked document affirms what I've said? Thank you again for the link; a concise and very useful find. Kind regards, Eliott
|
Next
|
Last
Pages: 1 2 3 Prev: Java reg exp question Next: Example of building a list of file names? |