เราสามารถประกาศอินเทอร์เฟซในอินเทอร์เฟซอีกตัวหนึ่งหรือในคลาสได้ อินเทอร์เฟซแบบนี้เรียกว่า Nested interface
กฎต่อไปนี้ควบคุม Nested interface:
- Nested interface ที่ประกาศภายในอินเทอร์เฟซ ต้องเป็น
public - Nested interface ที่ประกาศภายในคลาส สามารถมีตัวปรับการเข้าถึงได้ทุกตัว
- Nested interface เป็น
staticโดยค่าเริ่มต้น
ตัวอย่างต่อไปนี้เป็นตัวอย่างของ Nested interface:
ตัวอย่าง
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(); } }ผลลัพธ์
Dogs can walk and run