Reading through "Thinking in Java" i stuck in ex:6 of Inner Classes chapter.
Exercise 6: (2) Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return.
This is my code:
IOne.java
interface
package intfpack;
public interface IOne{
void f();
}
COne.java
Class with protected inner class that implements the interface
package classpack;
import intfpack.*;
public class COne{
protected class Inner implements IOne{
public void f(){System.out.println("Inner class of COne");}
}
}
CTwo.java
Inheriting from class with protected inner class
package thirdpack;
import classpack.*;
import intfpack.*;
public class CTwo extends COne{
public IOne getInner(){
IOne io = new Inner();
return io;
}
public static void main(String[] args){
CTwo ct = new CTwo();
ct.getInner();
}
}
Copmiler says next:
javac CTwo.java
CTwo.java:9: Inner() has protected access in classpack.COne.Inner
IOne io = new Inner();
^
1 error
But the book says that i can access protected inner classes in derived class. Where is mistake?