Using Inner Interfaces

Inner classes are frequently used to reduce project clutter and to reduce external exposure of classes with localized usefulness. Inner interfaces are defined in a similar fashion and while they probably have narrower application, they can still be a very useful development tool.

If you use interfaces to define lists of application constants, you can use inner interfaces within the main interface body to organize the constants and reduce the need for classes to implement the entire set of constants:

  public interface MainBody {    public interface GroupOne {        public static final int ONE = 1;        public static final int TWO = 2;    }    public interface GroupTwo {        public static final int THREE = 3;        ...

Classes implement the inner interfaces using the qualified notation:

  public class Xyz implements MainBody.GroupOne {

Inner interfaces are also commonly used to define simple public interfaces, such as listeners, where the class defining the inner interface supports the addition of the listeners defined by the interface type. In this way, project clutter is reduced. Interestingly, the compiler supports self-implementation of inner interfaces:

  public class Abc implements Abc.MyInterface {    ...    public interface MyInterface {        ... 

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: