Tuesday, October 8, 2013

Java Interfaces


Inner Interfaces:
Declaring an interface inside another interface. We can take Entry interface declared in Map interface as an example.

One example of inner interface used in java standard library is java.util.Map and Java.util.Map.Entry. Here java.util.Map is used also as a namespace. Entry does not belong to the global scope, which means there are many other entities that are Entries and are not necessary Map’s entries. This indicates that Entry represents entries related to the Map.
 
Sample Code:
public interface Map {
 interface Entry{
  int getKey();
 }
 
 void clear();
}
 
Why Use Inner Interface?
There are several compelling reasons for using inner interface:
  • It is a way of logically grouping interfaces that are only used in one place.
  • It increases encapsulation.
  • Nested interfaces can lead to more readable and maintainable code
 

No comments:

Post a Comment