interface

22:10 ---
เครื่องมือติดต่อกับสิ่งที่ไม่มีความสัมพันธ์ต่อกัน
multiple inheritance คือ class สามารถ inherit จากinterfaceได้เพียง constant
แต่interface จะ inherit ทั้ง method และ constant จาก superinterfaces และแต่ละคลาสอาจมี interface เดียวกันได้

รูปแบบ interface


public interface ชื่อ interface extends superinterfaces
{
Interface body
}

public= แสดงว่าอินเตอร์เฟสสามารถใช้คลาสหรือแพ็กเกตใดๆ ก้ได้ ถ้าไม่ระบุ publicไว้  อินเตอร์เฟสใช้ได้เฉพาะคลาสที่อยู่ในอินเตอร์เฟซ

extend= สามารถ extendได้หลายinterface มี,คั่น

interface โดยใช้คลาส

public class ชื่อclass implements ชื่อInterface
{
......
}

ถ้าเราใช้ abstrace class แทน interface จะไม่สามารถ inherit classอื่นได้อีก เพราะมีข้อจำกัดว่า inheritได้class เดียว จริงๆแล้วเรามีclassที่ต้องinheritจาก sub class อยู่แล้ว

public interface MyInterface
{
Public void methodA();
Public void methodB();
}
pubilc class Myclass implement MyInterface
{
Public void methodA();
{
.......
}
Public void methodB();
{
.......
}
}

0 ความคิดเห็น:

แสดงความคิดเห็น