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.

2

6 Answers 6

18

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 {
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

10

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 {
        ...
    }

    ...
}

9 Comments

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..
@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
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.
Member classes of interfaces are implicitly public and static.
@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.
|
7
public interface A
{
    public void helloWorld();

    public static class B implements A{

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

        }

    }
}

Comments

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).

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.