Using Inner Interfaces

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

See also  Comparing different methods of testing your Infrastructure-as-Code

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist