In a single .Java file, is it possible to have a public interface and a public class (which implements the interface)
I'm new to Java coding and it is written on most places on the net that a .java file cannot contain more than 2 public class. I want to know if it is true for the interface and a class also.
解决方案
No, it's not possible. There may be at most one top-level public type per .java file. JLS 7.6. Top Level Type Declarations states the following:
[…] there must be at most one [top level public] type per compilation unit.
You could however have a package-protected class in the same file. This compiles fine (if you put it in a file named Test.java:
public interface Test {
// ...
}
class TestClass implements Test {
// ...
}