We can declare an interface in another interface or class. Such an interface is termed as a nested interface.
The following are the rules governing a nested interface.
- A nested interface declared within an interface must be public.
- A nested interface declared within a class can have any access modifier.
- A nested interface is by default static.
Following is an example of a nested interface.
Example
class Animal {
interface Activity {
void move();
}
}
class Dog implements Animal.Activity {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class Tester {
public static void main(String args[]) {
Dog dog = new Dog();
dog.move();
}
}Output
Dogs can walk and run