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

# Inheritance

ধরো, তুমি একটা **Animal Kingdom** নিয়ে ভাবছো।

* **Animal** হলো base class।
* **Dog, Cat, Bird** হলো subclasses।
* সব Animals-এর কিছু common behavior আছে যেমন `eat()`, `sleep()`, `move()`।
* Subclass শুধু তার **specific behavior** add করে।

সহজ কথায়: **Inheritance হলো class-এর behavior reuse করা এবং নতুন class তৈরি করা।**

***

### **Inheritance কি?**

**Inheritance** হলো OOP-এর একটি principle, যেখানে একটি class (child/subclass) **অন্য class (parent/superclass) থেকে properties এবং methods** inherit করে।

* **Parent Class / Superclass:** Common behavior define করে।
* **Child Class / Subclass:** Parent behavior inherit করে + নিজের behavior add করতে পারে।

**Key Points:**

1. Code reuse
2. Logical hierarchy
3. Easy maintenance

***

### **Inheritance এর ধরন**

1. **Single Inheritance** – এক parent, এক child
2. **Multilevel Inheritance** – Grandparent → Parent → Child
3. **Hierarchical Inheritance** – এক parent, multiple children
4. **Multiple Inheritance** – এক child multiple parents (PHP supports via interfaces, not direct multiple class inheritance)

***

### **Single Inheritance**

{% code lineNumbers="true" %}

```php
<?php
class Animal {
    public $name;

    public function eat() {
        echo $this->name . " is eating\n";
    }

    public function sleep() {
        echo $this->name . " is sleeping\n";
    }
}

class Dog extends Animal {
    public function bark() {
        echo $this->name . " is barking\n";
    }
}

// Usage
$dog = new Dog();
$dog->name = "Tommy";
$dog->eat();    // inherited
$dog->sleep();  // inherited
$dog->bark();   // own method
?>
```

{% endcode %}

💡 **Benefit:**

* Code reuse → `eat()` এবং `sleep()` parent থেকে সরাসরি use করা হলো।
* Child class specific behavior add করা সহজ।

***

### **Multilevel Inheritance**

{% code lineNumbers="true" %}

```php
<?php
class Animal {
    public function eat() {
        echo "Animal is eating\n";
    }
}

class Mammal extends Animal {
    public function walk() {
        echo "Mammal is walking\n";
    }
}

class Dog extends Mammal {
    public function bark() {
        echo "Dog is barking\n";
    }
}

// Usage
$dog = new Dog();
$dog->eat();   // from Animal
$dog->walk();  // from Mammal
$dog->bark();  // from Dog
?>
```

{% endcode %}

💡 **Benefit:**

* Multi-level hierarchy create করা।
* Behavior step-by-step inherited।

***

### **Hierarchical Inheritance**

{% code lineNumbers="true" %}

```php
<?php
class Vehicle {
    public function start() {
        echo "Vehicle started\n";
    }
}

class Car extends Vehicle {
    public function openTrunk() {
        echo "Car trunk opened\n";
    }
}

class Bike extends Vehicle {
    public function kickStart() {
        echo "Bike kickstarted\n";
    }
}

// Usage
$car = new Car();
$car->start();      // inherited
$car->openTrunk();  // own

$bike = new Bike();
$bike->start();     // inherited
$bike->kickStart(); // own
?>
```

{% endcode %}

💡 **Benefit:**

* এক parent multiple children → shared behavior reuse করা।

***

### **Multiple Inheritance via Interface**

PHP direct multiple class inheritance support করে না। কিন্তু **interface use করে multiple inheritance achieve করা যায়।**

{% code lineNumbers="true" %}

```php
<?php
interface Flyable {
    public function fly();
}

interface Swimmable {
    public function swim();
}

class Duck implements Flyable, Swimmable {
    public function fly() {
        echo "Duck is flying\n";
    }
    public function swim() {
        echo "Duck is swimming\n";
    }
}

// Usage
$duck = new Duck();
$duck->fly();
$duck->swim();
?>
```

{% endcode %}

💡 **Benefit:**

* One class multiple behaviors inherit করতে পারে।
* Flexible + maintainable।

***
