The Artima Developer Community
Sponsored Link

Java Answers Forum
Trouble with protected class access

1 reply on 1 page. Most recent reply: Nov 14, 2003 2:20 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Steve

Posts: 5
Nickname: srbeckle
Registered: Nov, 2003

Trouble with protected class access Posted: Nov 14, 2003 7:06 AM
Reply to this message Reply
Advertisement
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?


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Trouble with protected class access Posted: Nov 14, 2003 2:20 PM
Reply to this message Reply
Add a public constructor for Fly inner class and user JDK1.4 and it will work.

Flat View: This topic has 1 reply on 1 page
Topic: Script conflict ! Help ! Previous Topic   Next Topic Topic: https to http

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use