views:

114

answers:

7

Is it possible to have an inner class inside the interface in java ???

+1  A: 

Yes, it is possible but it is not common practice.

interface Test
{
    class Inner
    { }
}

class TestImpl implements Test
{
    public static void main(String[] arg)
    {
        Inner inner = new Inner();
    }
}
Darin Dimitrov
Thank you very much
Renuka
+3  A: 

You can. But here's what O'Reilly says about it:

Nested Classes in Interfaces?

Java supports the concept of nested classes in interfaces. The syntax and dynamics work just like nested classes declared in a class. However, declaring a class nested inside an interface would be extremely bad programming. An interface is an abstraction of a concept, not an implementation of one. Therefore, implementation details should be left out of interfaces. Remember, just because you can cut off your hand with a saw doesn't mean that it's a particularly good idea.


That said, I could see an argument for a static utility class nested into an interface. Though why it would need to be nested into the interface instead of being a stand-alone class is completely subjective.

jdmichal
I don't agree. An complex "reference" interface may make use of simple value classes. Having said that, I'm not keen on public static nested classes.
Tom Hawtin - tackline
sometimes interface is just there for other reasons, some people over-use interfaces and each one only has one implementation. I think its a cool idea to mix it like that.
01
I generally agree that one should be careful with this, in not tying the interface to a particular implementation by nesting a class. But if the nested class has functionality that uses only the generic interface, and is specific to that interface and not some other functionality, I would have no arguments against it. In other words, as long as the interface could still be reused elsewhere without modifying the internal class, it's all groovy.
jdmichal
A: 

Yes. Straight from the language spec:

An inner class is a nested class that is not explicitly or implicitly declared static.

And (boldface mine):

A nested class is any class whose declaration occurs within the body of another class or interface.

Thomas
Thank you very much
Renuka
A: 

Doesn't answer your question directly, but on a related note you can also nest an interface inside another interface. This is acceptable, especially if you want to provide views. Java's collection classes do this, for example Map.java in the case of the Map.Entry view:

public interface Map<K,V> {
   ...
   public static interface Entry<K,V> {
       ....
   }
}

This is acceptable because you're not mixing implementation details into your interface. You're only specifying another contract.

Vivin Paliath
I also use this technique to specify callback interfaces for another interface. Very handy it is.
jdmichal
A: 

One use case for this that I find quite useful is if you have a builder that creates an instance of the Interface. If the builder is a static member of the Interface, you can create an instance like this:

DigitalObject o = new DigitalObject.Builder(content).title(name).build();
Fabian Steeg
That would be an instance of an implementation of the interface. I am not seeing that as a good idea.
Tom Hawtin - tackline
@Tom Yes, an instance conforming to the Interface, but the concrete class can remain package private and completely hidden behind the API, as the builder return type is the Interface. The way I see it this specifies an Interface together with a way to create instances, without giving away any concrete implementation. I don't see any disadvantage compared to having the builder as a top-level type.
Fabian Steeg
A: 

It is legal, but I only really do it with nested interfaces (as already mentioned) or nested enums. For example:

public interface MyInterface {

    public enum Type { ONE, TWO, THREE }

    public Type getType();

    public enum Status { GOOD, BAD, UNKNOWN }

    public Status getStatus();

}
Sean
A: 

I agree that this should be generally rare, but I do like to use inner classes in interfaces for services when the interface method needs to return multiple pieces of information, as it's really part of the contract and not the implementation. For example:

public interface ComplexOperationService {

    ComplexOperationResponse doComplexOperation( String param1, Object param2 );

    public static class ComplexOperationResponse {
        public int completionCode;
        public String completionMessage;
        public List<Object> data;
        // Or use private members & getters if you like...
    }

}

Obviously this could be done in a separate class as well, but to me it feels like I'm keeping the whole API defined by the interface in one spot, rather than spread out.

jefflub