Home  >  Article  >  Java  >  Nested interfaces in Java

Nested interfaces in Java

WBOY
WBOYforward
2023-09-09 23:01:02735browse

Nested interfaces in Java

We can declare an interface within another interface or class. Such interfaces are called nested interfaces.

The following are the rules for managing nested interfaces.

  • The interface within the declared nested interface interface must be public.
  • Nested interfaces declared within a class can have any access modifier.
  • Nested interfaces are static by default.

The following is an example of nested interfaces.

Example

Live Demonstration

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

The above is the detailed content of Nested interfaces in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete