Nested interface in Java


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

Live Demo

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

Updated on: 17-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements