Home > Java > javaTutorial > Nested interfaces in Java

Nested interfaces in Java

WBOY
Release: 2023-09-09 23:01:02
forward
783 people have browsed it

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();
   }
}
Copy after login

Output

Dogs can walk and run
Copy after login

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template