មេរៀនទី៥៨: Nested Interface

Interface មួយដែល ត្រូវបាន  ជាមួយ interface ឬ class ផ្សេងទៀត គឺជា nested interface។  nested interfaces ត្រូវបានប្រើ group បានទាក់ទង interfaces ដែលវាយអាចងាយស្រួល maintain។
Syntax នៃ nested interface ដែលត្រូវបានប្រកាសក្នុង interface
interface interface_name{

interface nested_interface_name{

}
}
Syntax នៃ nested interface ដែលត្រូវបានប្រកាសក្នុង class
class class_name{

interface nested_interface_name{

}
}
ឧទាហរណ៍នៃ nested interface ដែលត្រូវបានប្រកាសជាមួយ interface
យើងកំពុងរៀនពីរបៀបប្រកាស nested interface និងរបៀបចូលវា.
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println(“Hello nested interface”);}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
Download ឧទាហរណ៍កូដ interface
Output:hello nested interface
Internal code បង្កើតដោយ java compiler សម្រាប់ nested interface Message
:.
public static interface Showable$Message
{
public abstract void msg();
}
ឧទាហរណ៍នៃ nested interface ដែលបានប្រកាសនៅក្នុង class
class A{
interface Message{
void msg();
}
}
class TestNestedInterface2 implements A.Message{
public void msg(){System.out.println(“Hello nested interface”);}
public static void main(String args[]){
A.Message message=new TestNestedInterface2();//upcasting here
message.msg();
}
}
Output:hello nested interface
យើងអាចកំណត់ class ក្នុង interface ?
ប្រសិនបើកំណត់ class ខាងក្នុង interface, java compiler បង្កើត static nested class
interface M{
class A{}
}