I'm trying to implement an interface in a protected inner class, and can't get it to compile. The interface, base class, and derived class are in 3 separate packages as follows:
==== package p1;
public interface CanFly { public void fly(); }
===== package p2; import p1.CanFly;
public class Plane { protected class Fly implements CanFly { public void fly() {} } } ==== package p3; import p1.CanFly; import p2.Plane;
public class Jet extends Plane { //!! Does not compile.Note upcast to an interface public CanFly abc() { return new Fly(); } } =====
Since Fly is protected in Plane, I was under the impression it's available to the derived class Jet, even if Jet is in a separate package. Here's the error:
package3\Jet.java:6: Fly() has protected access in package2.Plane.Fly public CanFly abc() { return new Fly(); } ^ I have verified that I can provide a method in the base class Jet that does what I want, but the exercise I'm working on in Eckel's book implies that I should be able to define a method in the derived class that returns an object of the (inherited, but seemingly inaccessible) protected class. What am I missing?