Is it good to have a class nested inside an interface?

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

+3
source share
7 answers

You can. But here is what O'Reilly says :

Nested classes in interfaces?

Java supports the concept of nested classes in interfaces. Syntax and dynamics work just like nested classes declared in a class. However, declaring a class nested in an interface would be extremely poor programming. An interface is an abstraction of a concept, not its implementation. Therefore, implementation details should be excluded from the interfaces. Remember that just because you can cut off your hand with a saw does not mean that it is a particularly good idea.


However, I could see the argument for the static utility class nested in the interface. Although why it would have to be invested in an interface, and not be an autonomous class, this is completely subjective.

+3
source

, , , , , , , . :

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

}

, , , API, , , .

+2

, , .

interface Test
{
    class Inner
    { }
}

class TestImpl implements Test
{
    public static void main(String[] arg)
    {
        Inner inner = new Inner();
    }
}
+1

, . , . Java- , Map.java Map.Entry:

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

, . .

+1

. :

- , static.

( ):

, .

0

, , - , . , :

DigitalObject o = new DigitalObject.Builder(content).title(name).build();
0

, ( ) . :

public interface MyInterface {

    public enum Type { ONE, TWO, THREE }

    public Type getType();

    public enum Status { GOOD, BAD, UNKNOWN }

    public Status getStatus();

}
0

Source: https://habr.com/ru/post/1757563/


All Articles