3

I have a class:

package Member;
public class Player implements Character{
...
}

and I have interface that is not inside the package:

public interface Character{
...
}

I think that public intefaces and classes are visible to each other no matter if they are in the same package, so why I can't implement Character in the Player class? I have error: Cannot resolve symbol 'Character'

Pawlinho
  • 69
  • 1
  • 7
  • 2
    Did you import the interface? Or is the interface in the unnamed package? – Mark Rotteveel Apr 30 '21 at 08:47
  • 4
    You can't access types in the [unnamed package](https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.4.2) from any other package. Put them into a named package instead (i.e. have *some* `package` statement in the secon file). – Joachim Sauer Apr 30 '21 at 08:48
  • Related, possibly duplicate: [Import package with no name Java](https://stackoverflow.com/questions/33645950/import-package-with-no-name-java), [How to access a class whose .java file has no package statement?](https://stackoverflow.com/questions/23494084/how-to-access-a-class-whose-java-file-has-no-package-statement), [Java Packages - refer to a class from a different package](https://stackoverflow.com/questions/14592738/java-packages-refer-to-a-class-from-a-different-package) – Mark Rotteveel Apr 30 '21 at 08:49
  • @JoachimSauer The only solution is to put interface inside a named package? – Pawlinho Apr 30 '21 at 08:56
  • @Pawlinho: yes, the unnamed package shouldn't be used for anything but the simplest test programs (as described in the link I gave above). – Joachim Sauer Apr 30 '21 at 08:58

1 Answers1

0

If the interface (or any other class/enum you use) are in a different package, you need to either fully qualify it:

public class Player implements org.somepackage.Character {

or import it:

import org.somepackage.Character;
public class Player implements Character {
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • This is not incorrect, but I think OPs problem is that `Character` is in the unnamed package. He won't be able to import that. – Joachim Sauer Apr 30 '21 at 08:49
  • 1
    @JoachimSauer I think OP's confusion is that they thought "visible to other packages" meant "not even imports are needed". – Sweeper Apr 30 '21 at 08:50