មេរៀនទី២៣: Interface ក្នុង Java
interface ក្នុង java គឺជា blueprint នៃ class មួយ។ វាមាន static constants និង abstract methods។
interface ក្នុង java គឺជា mechanism to achieve fully abstraction។ មាន abstract methods មួយគត់នៅក្នុង java interface មិនមែន method body។
ហេតុអ្វីប្រើ Java interface?
មានបីហេតុផលដើម្បីប្រើ interface
• វាត្រូវបានប្រើដើម្បី abstraction
• ដោយប្រើinterface ផ្គត់ផ្គង់អនុគមន៍ inheritance។
java compiler បន្ថែម abstract keywords មុនពេលប្រើ interface method និងpublic, static និងfinal keywords។

ស្វែងយល់ ពីទំនាក់ទំង relationship រវាង classes និង interfaces
ដូចដែលបានបង្ហាញក្នុងរូបភាពខាងក្រោម
ឧទាហរណ៍នៃ Java interface
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println(“Hello”);}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Output:Hello
inheritance ច្រើនក្នុង Java ដោយប្រើ interface
ប្រសិនបើ class មួយអនុវត្តន៍ ច្រើន interfaces, ឬ interface extends ច្រើន interfaces ឧទាហរណ៍
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println(“Hello”);}
public void show(){System.out.println(“Welcome”);}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:Hello
Welcome
Interface inheritance
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class Testinterface2 implements Showable{
public void print(){System.out.println(“Hello”);}
public void show(){System.out.println(“Welcome”);}
public static void main(String args[]){
Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();
}
}
Hello
Welcome
Nested Interface ក្នុង Java
ចំណាំ: interface មួយអាចមាន interface ផ្សេងទៀត ឧទាហរណ៍ nested interface
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
interface ក្នុង java គឺជា mechanism to achieve fully abstraction។ មាន abstract methods មួយគត់នៅក្នុង java interface មិនមែន method body។
ហេតុអ្វីប្រើ Java interface?
មានបីហេតុផលដើម្បីប្រើ interface
• វាត្រូវបានប្រើដើម្បី abstraction
• ដោយប្រើinterface ផ្គត់ផ្គង់អនុគមន៍ inheritance។
java compiler បន្ថែម abstract keywords មុនពេលប្រើ interface method និងpublic, static និងfinal keywords។

ស្វែងយល់ ពីទំនាក់ទំង relationship រវាង classes និង interfaces
ដូចដែលបានបង្ហាញក្នុងរូបភាពខាងក្រោម

interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println(“Hello”);}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Output:Hello
inheritance ច្រើនក្នុង Java ដោយប្រើ interface
ប្រសិនបើ class មួយអនុវត្តន៍ ច្រើន interfaces, ឬ interface extends ច្រើន interfaces ឧទាហរណ៍

void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println(“Hello”);}
public void show(){System.out.println(“Welcome”);}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:Hello
Welcome
Interface inheritance
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class Testinterface2 implements Showable{
public void print(){System.out.println(“Hello”);}
public void show(){System.out.println(“Welcome”);}
public static void main(String args[]){
Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();
}
}
Hello
Welcome
Nested Interface ក្នុង Java
ចំណាំ: interface មួយអាចមាន interface ផ្សេងទៀត ឧទាហរណ៍ nested interface
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
Post a Comment