Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Inheritance (Enhanced)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Inheritance Visual Guide
Vehicle → Car / Bike လို parent-child relationship ကို OOP visual ထဲမှာမြင်နိုင်ပါတယ်။

🐍 Lesson 3: Inheritance

1. Inheritance ဆိုတာဘာလဲ?

မြန်မာ → Inheritance ဆိုတာ parent class (base class) က code တွေကို child class (derived class) မှာ ပြန်သုံးနိုင်တဲ့ OOP concept တစ်ခု။

English → Inheritance allows a child class to reuse and extend the functionality of a parent class.

2. Why Inheritance?

  • Reuse existing code
  • Avoid duplication
  • Extend functionality easily
  • Model real-world hierarchies (e.g., Animal → Dog, Cat)

3. အကျဉ်းချုပ်

✅ Inheritance = reuse + extend parent class

super() → call parent methods

✅ Supports single, multiple, multilevel, hierarchical inheritance

✅ Helps model real-world hierarchies

python
# ===== 1. Parent class =====
class Animal:
    def __init__(self, name):
        self.name = name
    
    def speak(self):
        return f"{self.name} makes a sound."

# ===== 2. Child class =====
class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

class Cat(Animal):
    def speak(self):
        return f"{self.name} says Meow!"

# ===== 3. Create objects =====
dog = Dog("Bobby")
cat = Cat("Kitty")

print(dog.speak())  # Bobby says Woof!
print(cat.speak())  # Kitty says Meow!

# ===== 4. super() Keyword =====
class Bird(Animal):
    def __init__(self, name, can_fly=True):
        super().__init__(name)   # call parent constructor
        self.can_fly = can_fly

bird = Bird("Eagle")
print(f"\n{bird.speak()}")  # Eagle makes a sound.

# ===== 5. Multiple Inheritance =====
class Flyer:
    def fly(self):
        return "I can fly!"

class Swimmer:
    def swim(self):
        return "I can swim!"

class Duck(Flyer, Swimmer):
    pass

d = Duck()
print(f"\n{d.fly()}")   # I can fly!
print(d.swim())  # I can swim!
You should see
Bobby says Woof! Kitty says Meow! Eagle makes a sound. I can fly! I can swim!
Inheritance (Enhanced) | Thuta Learning