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

Inheritance

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

Inheritance သည် parent class မှ properties နှင့် methods များကို child class က inherit လုပ်နိုင်စေသည်။ extends keyword နှင့် super() ကို အသုံးပြုသည်။

📚 Benefits:

• Code reusability

• Hierarchical relationships

• Method overriding

• Extend functionality

javascript
class Animal {
    constructor(name) {
        this.name = name;
    }
    
    speak() {
        return `${this.name} makes a sound`;
    }
}

class Dog extends Animal {
    constructor(name, breed) {
        super(name);  // Call parent constructor
        this.breed = breed;
    }
    
    speak() {
        return `${this.name} barks: Woof!`;
    }
    
    fetch() {
        return `${this.name} is fetching the ball`;
    }
}

class Cat extends Animal {
    constructor(name, color) {
        super(name);
        this.color = color;
    }
    
    speak() {
        return `${this.name} meows: Meow!`;
    }
}

const dog = new Dog("Buddy", "Golden Retriever");
const cat = new Cat("Whiskers", "Orange");

console.log(dog.speak());
console.log(dog.fetch());
console.log(cat.speak());
You should see
Buddy barks: Woof! Buddy is fetching the ball Whiskers meows: Meow!
Inheritance | Thuta Learning