Alex Rivera | Logout

Can we define an interface within an interface?

Asked 2010-02-25T06:42:40.057
18

I like to know can we define an interface within an interface. like

interface abc {
    void show();
    public interface xyz {
        void read();
    }
}

This was question asked in interview. Any practical use of this.

Edit
Report

1 Answer

12

Sure.. Look at SOURCE CODE for java.util.Map interface. Map interface contains a nested Entry interface.

Interestingly, in the source code it simply says

interface Entry <K,V> {
  ..
}

but the javadoc says

public static interface Map.Entry<K,V>

I guess this is because nested interfaces are implicitly "public static" even though the source code doesn't say that. (But methods inside an interface are implicity public, and cannot be static,that is, only instance methods are permitted in interfaces).

-dbednar 2013-07-02

answered 2013-07-02T18:01:56.837

Your Answer