Thuta Learning
IntermediateProgrammingbeginner

Inheritance

Relax. We'll talk through this in plain words — no textbook voice.

Inheritance lets a child class inherit properties and methods from a parent class. extends keyword and super() are used for this.

📚 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