> 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/polymorphism.md).

# Polymorphism

ধরো তুমি **remote control** ব্যবহার করছো:

* Remote control দিয়ে তুমি **TV, AC, Fan** সব operate করতে পারো।
* তুমি জানো কিভাবে button চাপলে device কাজ করবে, কিন্তু প্রতিটি device কাজের ভিতরের logic ভিন্ন।

সহজ কথায়: **Polymorphism হলো একই interface দিয়ে বিভিন্ন object কে handle করা।**

***

### **Polymorphism কি?**

**Polymorphism** হলো OOP-এর একটি principle, যেখানে **একই method call বা interface** বিভিন্ন class অনুযায়ী **ভিন্নভাবে কাজ করে।**

* Greek meaning: *poly = many, morph = forms* → Many forms of same behavior
* মূল idea: **একটি interface, multiple implementation**

***

### **Polymorphism এর ধরন**

1. **Compile-time Polymorphism / Static Polymorphism**
   * **Method Overloading / Operator Overloading**
   * PHP-তে সরাসরি method overloading নেই, তবে **default parameters / variadic functions** ব্যবহার করা যায়।
2. **Run-time Polymorphism / Dynamic Polymorphism**
   * **Method Overriding**
   * Child class parent class এর method override করে behavior পরিবর্তন করে।
   * PHP-এ abstract class এবং interface দিয়ে achieve করা যায়।

***

### **Run-time Polymorphism (Method Overriding)**

{% code lineNumbers="true" %}

```php
<?php
class Animal {
    public function makeSound() {
        echo "Some generic animal sound\n";
    }
}

class Dog extends Animal {
    public function makeSound() {
        echo "Bark Bark\n";
    }
}

class Cat extends Animal {
    public function makeSound() {
        echo "Meow Meow\n";
    }
}

// Usage
$animals = [new Animal(), new Dog(), new Cat()];

foreach ($animals as $animal) {
    $animal->makeSound(); // different behavior based on object
}
?>
```

{% endcode %}

**Benefit:**

* Parent reference দিয়ে child objects handle করা যায়।
* Flexible, maintainable, loosely coupled।

***

### **Polymorphism via Interface**

{% 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:**

* এক interface দিয়ে multiple implementations handle করা যায়।
* External code change না করে new payment gateway add করা যায়।

***

### **Compile-time Polymorphism via Default Parameters**

```php
<?php
class Calculator {
    public function add($a, $b, $c = 0) {
        return $a + $b + $c;
    }
}

// Usage
$calc = new Calculator();
echo $calc->add(5, 10);      // 15
echo $calc->add(5, 10, 20);  // 35
?>
```

**Benefit:**

* একই method name different parameter count অনুযায়ী behave করে।
* PHP limited support, run-time polymorphism বেশি common।

***

### **Key Points of Polymorphism**

1. **Many forms of the same behavior.**
2. **Achieved via:**
   * Method Overriding (Run-time)
   * Interface implementation
   * Method overloading / default params (Compile-time)
3. **Supports:**
   * Loose coupling
   * Code flexibility
   * Maintainability
4. **Parent reference → Child object behavior:**
   * `$animal = new Dog(); $animal->makeSound();`
