开发者

Can't access Classes in default package

I have a problem with the default package. Basically my project structure consists of three main classes which extend one abstract class in a package called simulation. Yesterday I did a name change in the project and now my three main classes are put automatically in the default package, so they can't be visible by the class inside the simulation package and vice versa. For the following code

import simulation.*;

class SSQSim extends Simulation{

} 

I get these errors "开发者_开发百科This class must implement the inherited abstract method Simulation.stop(), but cannot override it since it is not visible from SSQSim. Either make the type abstract or make the inherited method visible" "The type Simulation is not visible"

Thank you in advance.

EDIT The problem is that I don't have to use any other packages. It's basically homework and the rules for the submission are pretty strict: First, I have to submit the package "simulation" which contains the abstract class "Simulation" with some methods to implement with the help of the other classes. This part is fine.

Then, I have to create three classes which import the package "simulation" and extend its class "Simulation". They have specifically said not to put these classes in any package. At first, they all worked fine but after I renamed the project. These classes suddenly moved into the default package and now they give me these errors.


How are you creating your classes? When you create a new class, Eclipse shows a New Java Class dialog. This dialog lets you choose which package to create your new class in:

Can't access Classes in default package

Just click the Browse button and you'll be able to pick a package.

Can't access Classes in default package


Why you use the default package,From the java specs:

It is a compile time error to import a type from the unnamed package.

So you have to create a new package with a different name and add your classes or put them in the same package as your class and do the required imports.


This is due to the fact that the signature of Simulation.stop() method mentions some class that is package-restricted (non-public) to the simulation package.

You solve it by either

  1. Move SSQSim to the simulation package so it can access the same classes as Simulation, or
  2. Make the classes required to extend the Simulation class package-public.

To move a class from one package to another in eclipse, you can simply drag the source-code file into the desired package, and eclipse will refactor the code accordingly.


(A side-note: As a rule of thumb, don't use the default package. The problem you ran into however, can occur even if you avoid the default package!)


This is just a hint for now, as the source code for all the classes is not posted. The error message implies the presence of an abstract method in the parent class, that cannot be overriden due to loss in visibility in the child class. Usually, this is due to the use of the default access specifier ("friendly"), or the private access specifier (which is meaningless, especially if the method is declared as abstract). One will have to use the protected access modifier on the parent, to ensure that the method is now visible to all child classes irrespective of whether they are present in the same package or not.

In short, if the child cannot "see" the method that is being overriden, then the compiler issues a warning that informs about the inevitability to deduce whether the method defined in the parent or the child should used at runtime.

As far as Eclipse's behavior of creating classes in the default package is concerned, one can always ensure that a package is specified when creating a new class in the appropriate dialog (the new Java Class dialog, where a package field is present).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜