Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Nested interface trong Java


Chúng ta có thể khai báo interface trong interface hoặc class khác. Interface như vậy gọi là nested interface (interface lồng nhau).

Các quy tắc quản lý nested interface:

  • Nested interface khai báo trong interface phải là public.
  • Nested interface khai báo trong class có thể có bất kỳ access modifier nào.
  • Nested interface mặc định là static.

Ví dụ nested interface:

Ví dụ

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

Kết quả

Dogs can walk and run