> For the complete documentation index, see [llms.txt](https://mominur.gitbook.io/mominur-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mominur.gitbook.io/mominur-notes/system-design/oop/abstraction.md).

# Abstraction

ধরো তুমি **Car চালাচ্ছো**।

* তুমি শুধু gas pedal, brake, steering wheel ব্যবহার করছো।
* Engine, gearbox, fuel injection system, brake hydraulics—all hidden।
* তুমি জানো না এই সব system কিভাবে কাজ করে।

সহজ কথায়: তুমি **কি করতে পারবে** জানো, কিন্তু **কিভাবে হচ্ছে তা জানো না।**

* **Car** হলো **class**
* **Interface / Public methods** হলো pedals, steering, buttons
* **Internal engine/gear system** হলো hidden implementation
* **এটাই হলো Abstraction।**

***

### **Abstraction কি?**

**Abstraction** হলো OOP-এর একটি principle যা **complex systems থেকে unnecessary details hide করে**, এবং **essential behavior/interface provide করে।**

* **Main idea:** **What it does**, not how it does it.
* **Focus:** Interface and essential functionality, internal implementation hide।

***

### **Abstraction vs Encapsulation**

<table><thead><tr><th width="122">Feature</th><th width="300">Encapsulation</th><th>Abstraction</th></tr></thead><tbody><tr><td>Focus</td><td>Hide data</td><td>Hide implementation details</td></tr><tr><td>Concept</td><td>Private properties + getter/setter</td><td>Abstract class / Interface / abstract methods</td></tr><tr><td>Purpose</td><td>Controlled access</td><td>Reduce complexity &#x26; show essential behavior</td></tr><tr><td>Example</td><td>BankAccount balance private</td><td>Payment gateway interface</td></tr></tbody></table>

***

### **Abstraction কেন প্রয়োজন?**

1. **Reduce Complexity** – Users/internal code শুধু essential behavior দেখে।
2. **Maintainability** – Implementation change করলে external code break হয় না।
3. **Reusability** – Abstract classes/interfaces reuse করা যায়।
4. **Polymorphism support** – এক interface দিয়ে বিভিন্ন implementation handle করা যায়।

***

### **Abstraction Implement করার উপায়**

PHP-এ **Abstract Class** এবং **Interface** দিয়ে Abstraction implement করা হয়।

***

### **1. Abstract Class**

* Abstract class partially implemented class, যা **abstract methods** declare করতে পারে।
* Child class অবশ্যই abstract methods implement করতে হবে।

**Example:**

{% code lineNumbers="true" %}

```php
<?php
abstract class Vehicle {
    protected $speed;

    public function setSpeed($speed) {
        $this->speed = $speed;
    }

    // Abstract method
    abstract public function move();
}

class Car extends Vehicle {
    public function move() {
        echo "Car is moving at " . $this->speed . " km/h\n";
    }
}

class Bike extends Vehicle {
    public function move() {
        echo "Bike is moving at " . $this->speed . " km/h\n";
    }
}

// Usage
$car = new Car();
$car->setSpeed(100);
$car->move();

$bike = new Bike();
$bike->setSpeed(50);
$bike->move();
?>
```

{% endcode %}

**Benefit:**

* Users শুধু `move()` call করবে, implementation hide।
* Code reusable + maintainable।

***

### **2. Interface**

* Interface শুধু method declare করে, কোন implementation নেই।
* Class implements interface করে এবং methods implement করে।

**Example:**

{% code lineNumbers="true" %}

```php
<?php
interface PaymentGateway {
    public function pay($amount);
}

class Paypal implements PaymentGateway {
    public function pay($amount) {
        echo "Paid $amount via PayPal\n";
    }
}

class Stripe implements PaymentGateway {
    public function pay($amount) {
        echo "Paid $amount via Stripe\n";
    }
}

// Usage
function processPayment(PaymentGateway $gateway, $amount) {
    $gateway->pay($amount);
}

$paypal = new Paypal();
$stripe = new Stripe();

processPayment($paypal, 1000);
processPayment($stripe, 500);
?>
```

{% endcode %}

**Benefit:**

* One interface multiple implementations possible.
* Code loosely coupled, flexible.

***

#### **3. Abstract Methods in Interface vs Abstract Class**

<table><thead><tr><th width="189">Feature</th><th>Abstract Class</th><th>Interface</th></tr></thead><tbody><tr><td>Implementation</td><td>Can have implemented methods</td><td>Only method signature (PHP 8+ can have default method)</td></tr><tr><td>Variables</td><td>Can have properties</td><td>No properties (PHP 8.1+ allows constants)</td></tr><tr><td>Multiple Inheritance</td><td>No (single inheritance)</td><td>Yes (class can implement multiple interfaces)</td></tr></tbody></table>

***
