Interface Methods ใน Java
Interface กำหนด Contract (สัญญา) ว่า Class ที่ implement ต้องมี method เหล่านี้
ประวัติการพัฒนา
| Version | Features |
|---|---|
| Java 7- | เฉพาะ public abstract methods (implicit) |
| Java 8 | + default methods, static methods |
| Java 9 | + private methods, private static methods |
ตัวอย่างโค้ด (Java 8+)
interface Car {
// Abstract method (implicit public abstract)
void carSpeed();
void sleep();
// Default method - มี implementation
default void honk() {
System.out.println("Beep beep!");
}
// Static method - เรียกผ่าน Interface name
static void describe() {
System.out.println("This is a Car interface");
}
// Private method (Java 9+) - helper สำหรับ default methods
private void log(String msg) {
System.out.println("[LOG] " + msg);
}
default void startEngine() {
log("Engine starting...");
System.out.println("Vroom!");
}
}
class Porsche implements Car {
@Override
public void carSpeed() {
System.out.println("The speed of the Porsche is too much");
}
@Override
public void sleep() {
System.out.println("Sleeping for few milliseconds");
}
// สามารถ override default method ได้
@Override
public void honk() {
System.out.println("Porsche horn: 🎵");
}
}
public class Demo {
public static void main(String[] args) {
Porsche myCar = new Porsche();
myCar.carSpeed();
myCar.sleep();
myCar.honk(); // ใช้ default หรือ overridden
myCar.startEngine(); // ใช้ default (เรียก private method ภายใน)
Car.describe(); // Static method เรียกผ่าน Interface
}
}
ผลลัพธ์
The speed of the Porsche is too much Sleeping for few milliseconds Porsche horn: 🎵 [LOG] Engine starting... Vroom! This is a Car interface
คำอธิบายแต่ละประเภท
1. Abstract Methods (Java 7+)
- ไม่มี body
- Implementing class ต้อง implement (หรือเป็น abstract class)
- Implicit:
public abstract
2. Default Methods (Java 8+)
- มี body (implementation)
- Class ที่ implement สามารถ override ได้ (แต่ไม่บังคับ)
- แก้ปัญหา "Interface Evolution" - เพิ่ม method ใหม่โดยไม่ทำลาย existing implementations
- Multiple Inheritance Conflict: ถ้า 2 interfaces มี default method ชื่อเดียวกัน → ต้อง override ใน class
3. Static Methods (Java 8+)
- เป็นของ Interface ไม่ใช่ Instance
- เรียกผ่าน
InterfaceName.method() - ไม่สามารถ override ได้ (แต่สามารถ hide ได้ใน sub-interface)
- ใช้สำหรับ Utility/Helper methods
4. Private Methods (Java 9+)
- เป็น Helper สำหรับ default methods ภายใน Interface เดียวกัน
- ไม่สืบทอด (Not inherited)
- ลด Code Duplication ใน default methods
Diamond Problem Resolution (Java 8)
interface A { default void foo() { System.out.println("A"); } }
interface B { default void foo() { System.out.println("B"); } }
class C implements A, B {
// ต้อง override เพื่อ resolve conflict
@Override
public void foo() {
A.super.foo(); // เรียก default ของ A โดยเฉพาะ
// หรือ B.super.foo();
}
}
Best Practices
- ใช้ default methods อย่างน้อย - เฉพาะเมื่อจำเป็นต้อง evolve interface
- Static methods สำหรับ utilities ที่เกี่ยวข้องกับ interface
- Private methods สำหรับ shared logic ระหว่าง default methods
- Document ว่า default method ควร override หรือไม่