17

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.

Linda Peters
  • 179
  • 1
  • 1
  • 3

6 Answers6

17

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 {
    // ...
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    [JLS §7.6 Top Level Type Declarations](http://java.sun.com/docs/books/jls/third_edition/html/packages.html#26783) – Matt Ball Aug 20 '11 at 17:52
9

You can have as many public classes as you want in one file if you use nested classes. In your example:

public interface I {
    public class C implements I {
        ...
    }

    public class D implements I {
        ...
    }

    ...
}
emboss
  • 38,880
  • 7
  • 101
  • 108
  • Exactly! I was about to post a similar answer. Could you please tell me what's the point of having such a structure though? If I try to instantiate class 'C' I am forced to re-implement the methods defined in 'I', even if I already did it when defying class 'C'! P.S. I guess 'C' doesn't need to be static.. – Marsellus Wallace Aug 20 '11 at 17:56
  • @Gevorg -- The static keyword means that its the same as a top-level class. The only difference here is that you can only refer to C as `I.C` – Kal Aug 20 '11 at 18:00
  • 1
    I often use this idiom if I define an interface 'I' and a "default implementation" of 'I' that is used if no special instance is needed. I like this because I can have them close - no need to scatter them among several files. And besides that it just reads awesome to say: `setParam(new Interface.Default());` If I didn't use an inner class there I had to think about a clever name, this way I can just call it `Default`. – emboss Aug 20 '11 at 18:04
  • 3
    Member classes of interfaces are implicitly public and static. – Nicola Musatti Aug 20 '11 at 18:08
  • @emboss -- One point. I think the word "inner" is confusing in the context of static classes. Typically, these are referred to nested classes. Also, I dont think you need public or static since its nested inside an interface. Good answer. +1. – Kal Aug 20 '11 at 18:09
  • @Nicola: You're right. Although I can declare the inner class as package-private, I can still access it from within a different package. `static` is also redundant. I'm just used to add it nevertheless, and use non-static inner classes only where I really intend to. OK if I leave it? – emboss Aug 20 '11 at 18:13
  • @Kal: OK, you convinced me - I'll edit taking your and Nicola's comments into account. – emboss Aug 20 '11 at 18:14
  • @emboss: Hmmm.. Given your example above, is there any theoretical/practical reason why I should prefer having 'C' defined within 'I'? Can I just keep 'C' in a separate file and be equally happy or even more? Thanks! (I'll review inner classes later today...) – Marsellus Wallace Aug 20 '11 at 18:17
  • @Gevorg: No problem with keeping it separate, I just like to "misuse" the outer interface for namespacing. If I had the default class I mentioned in the other comments in a separate file, then I would have to call it DefaultInterface, InterfaceDefault or something stupid. I hate spending too much thought on clever names - that's why I began using these nested default classes - I just have to name them 'Default' :) – emboss Aug 20 '11 at 18:22
6
public interface A
{
    public void helloWorld();

    public static class B implements A{

        @Override
        public void helloWorld() {
            System.out.print("Hello World");

        }

    }
}
Shawn
  • 7,235
  • 6
  • 33
  • 45
5

The Java rule is that only one public class or interface can show up in a source file, and the name must match the file (i.e. Test.java --> public class Test or public interface Test, but not both).

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
0

One also needs to understand interface driven programming as next step while understanding an interface. It tell what is actual use of interface. What role it plays in a Java (or any other language) program.

Deepak
  • 39
  • 2
0

Yes we can write both Interface as well as public class in a same java file

interface Interfable {
public void interfMethod();

}

public class TestLam {

int x = 5;

public void testLamMethod() {
    int y = 10;
    Interfable i = () -> {
        System.out.println(x);
        System.out.println(y);
    };
    i.interfMethod();
}

public static void main(String[] args) {

    TestLam t = new TestLam();
    t.testLamMethod();
}

}

output: 5

10

// NOTE .java file name should be same as class name