© Khmer Angkor Academy - sophearithput168

Abstraction

Abstraction (ការបំបាត់ភាពស្មុគស្មាញ) ក្នុង Java

🎯 Abstraction គឺជាអ្វី?

Abstraction គឺការលាក់ implementation details ហើយបង្ហាញតែ essential features ដល់អ្នកប្រើប្រាស់។ វា focus លើ "What it does" មិនមែន "How it does"

ឧទាហរណ៍ក្នុងជីវិតពិត:

  • ទូរស័ព្ទ: ចុចហៅ → មិនចាំបាច់ដឹងសៀគ្វីខាងក្នុង
  • រថយន្ត: បើកបរ → មិនចាំបាច់ដឹង engine ដំណើរការយ៉ាងណា
  • ATM: ដកលុយ → មិនដឹងប្រព័ន្ធខាងក្នុង
  • Remote TV: ចុចប៊ូតុង → មិនដឹង signal transmission

💡 ហេតុអ្វីត្រូវការ Abstraction?

  1. Simplicity: បង្ហាញតែអ្វីចាំបាច់ លាក់អ្វីស្មុគស្មាញ
  2. Reduce Complexity: អ្នកប្រើមិនចាំបាច់យល់ internal logic
  3. Security: លាក់ sensitive implementation
  4. Maintainability: ផ្លាស់ប្តូរ internal ដោយមិន affect external
  5. Code Organization: រៀបចំកូដជា layers
  6. Flexibility: Multiple implementations of same abstraction

📚 របៀបធ្វើ Abstraction ក្នុង Java

Java ផ្តល់ 2 វិធី:

  1. Abstract Classes (0-100% abstraction)
  2. Interfaces (100% abstraction)

🎨 Abstract Classes

📖 Abstract Class គឺជាអ្វី?

Abstract class គឺ class ដែល:

  • ប្រើ abstract keyword
  • មិនអាច instantiate (cannot create objects)
  • អាចមាន abstract methods (no body)
  • អាចមាន concrete methods (with body)
  • អាចមាន constructors, fields, normal methods

✅ Syntax

abstract class ClassName {
    // Abstract method (no body)
    abstract returnType methodName();
    
    // Concrete method (with body)
    void normalMethod() {
        // Implementation
    }
}

🔑 Abstract Method

Abstract method គឺ method ដែល:

  • មាន declaration តែមិនមាន implementation
  • ត្រូវតែស្ថិតក្នុង abstract class
  • Child class ត្រូវ override
abstract class Animal {
    // Abstract method
    abstract void makeSound();
    
    // Concrete method
    void sleep() {
        System.out.println("Zzz...");
    }
}

class Dog extends Animal {
    // Must override abstract method
    void makeSound() {
        System.out.println("Woof!");
    }
}

// Usage
Animal dog = new Dog();
dog.makeSound();  // Woof!
dog.sleep();      // Zzz...

⚠️ Rules for Abstract Classes

Rule Explanation
Cannot instantiate Animal a = new Animal(); // ❌ ERROR
Must override abstract methods Child class ត្រូវ implement all abstract methods
Can have constructors Called when child object is created
Can have any access modifier public, protected, private for methods/fields
Cannot be final abstract final class... // ❌ Contradictory
Java Code
Click "Run" to execute the Java code

🔷 Interfaces

📖 Interface គឺជាអ្វី?

Interface គឺ blueprint ដែល:

  • ផ្តល់ 100% abstraction
  • មានតែ abstract methods (before Java 8)
  • Java 8+: អាចមាន default និង static methods
  • Java 9+: អាចមាន private methods
  • Fields ទាំងអស់គឺ public static final (constants)

✅ Syntax

interface InterfaceName {
    // Abstract method (implicitly public abstract)
    void method1();
    
    // Constant (implicitly public static final)
    int MAX_VALUE = 100;
    
    // Default method (Java 8+)
    default void method2() {
        System.out.println("Default implementation");
    }
}

🔗 Implementing Interface

interface Drawable {
    void draw();  // abstract method
}

class Circle implements Drawable {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

class Rectangle implements Drawable {
    public void draw() {
        System.out.println("Drawing Rectangle");
    }
}

// Usage
Drawable shape1 = new Circle();
Drawable shape2 = new Rectangle();
shape1.draw();  // Drawing Circle
shape2.draw();  // Drawing Rectangle

🎯 Multiple Inheritance with Interfaces

Java មិនអនុញ្ញាត multiple inheritance ជាមួយ classes ប៉ុន្តែអនុញ្ញាតជាមួយ interfaces!

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

// Implement multiple interfaces
class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck flying");
    }
    
    public void swim() {
        System.out.println("Duck swimming");
    }
}

📊 Abstract Class vs Interface

លក្ខណៈ Abstract Class Interface
Keyword abstract class interface
Inheritance extends (single only) implements (multiple)
Methods Abstract + Concrete Abstract (+ default/static in Java 8+)
Variables Any type public static final only
Constructor ✅ Yes ❌ No
Access Modifiers Any public only
Abstraction 0-100% 100%
When to use IS-A relationship CAN-DO capability
Java Code
Click "Run" to execute the Java code

💡 When to Use What?

✅ Use Abstract Class when:

  • មាន shared code (common implementation)
  • ត្រូវការ constructor
  • ត្រូវការ instance variables
  • មាន IS-A relationship (Dog IS-A Animal)
  • ត្រូវការ protected members

✅ Use Interface when:

  • ត្រូវការ multiple inheritance
  • Define contract/capability (CAN-DO)
  • Unrelated classes អាច implement
  • Define constants
  • ត្រូវការ 100% abstraction

🎯 Real-World Examples

Abstract Class Example - Vehicle System:

abstract class Vehicle {
    String brand;
    
    abstract void start();  // Different for each type
    
    void stop() {  // Same for all
        System.out.println("Vehicle stopped");
    }
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car starts with key");
    }
}

class Motorcycle extends Vehicle {
    void start() {
        System.out.println("Motorcycle starts with button");
    }
}

Interface Example - Payment System:

interface Payable {
    void processPayment(double amount);
}

class CreditCard implements Payable {
    public void processPayment(double amount) {
        System.out.println("Paid $" + amount + " via Credit Card");
    }
}

class PayPal implements Payable {
    public void processPayment(double amount) {
        System.out.println("Paid $" + amount + " via PayPal");
    }
}

// Different classes, same capability!

✅ Best Practices

  1. Favor composition over inheritance: មិនត្រូវ abuse abstraction
  2. Keep interfaces small: Interface Segregation Principle
  3. Name interfaces appropriately: -able suffix (Drawable, Comparable)
  4. Don't overuse abstraction: តែពេលចាំបាច់ប៉ុណ្ណោះ
  5. Document abstract methods: ពន្យល់អំពីអ្វីដែល subclass ត្រូវធ្វើ

⚠️ Common Mistakes

កំហុស ហេតុផល ដំណោះស្រាយ
new Animal() Cannot instantiate abstract class Create concrete subclass
Abstract method with body Abstract = no implementation Remove abstract or add body
Forget to override Must implement all abstract methods Override all in child class
Use class for multiple inheritance Java មិនអនុញ្ញាត Use interfaces instead
Java Code
Click "Run" to execute the Java code