I am currently reading Thinking in Java, and I ran into one small problem. I am doing exercise 12 of chapter 8.
Create an interface with at least one method in your own package. Create the class in a separate package. Add a secure inner class that implements the interface. In the third> package, inherit from your class and inside the method return an object of the protected> inner class, which increases the level of display in the interface during the return.
So, I created these .java files:
A.java
package c08; public interface A { void one(); }
Pr2.java
package c082; import c08.*; public class Pr2 { protected class InPr2 implements A { public void one() {System.out.println("Pr2.InPr2.one");} protected InPr2() {} } }
Ex.java
package c083; import c082.*; import c08.*; class Cl extends Pr2 { A foo() { InPr2 bar=new InPr2(); return bar; } }
And my NetBeans IDE emphasizes
InPr2();
and says that: InPr2 () has secure access in C082.Pr2.InPr2, and I wonder why. If I didn’t explicitly indicate that the InPr2 constructor should be protected, it will be available only in the C082 package, but when I inherit the Pr2 shoudn't class, will it be available in the Cl class because InPr2 is protected? Everything is fine when I change the constructor to public.
Andrew
source share