Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Mar 19, 2001

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 {
        ...
 
Andy Brundell
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date