Inheritance in OOP

Understanding code reuse and hierarchical relationships in Object-Oriented Programming

What is Inheritance?

Inheritance is a fundamental OOP concept that allows a class to inherit properties and methods from another class. The class that inherits is called the derived class (or child class), and the class being inherited from is called the base class (or parent class).

Inheritance क्या है? (हिंदी में)

Inheritance OOP का एक महत्वपूर्ण सिद्धांत है जो एक class को दूसरी class के properties और methods को inherit करने की अनुमति देता है। जो class inherit करती है उसे derived class (या child class) कहते हैं, और जिस class से inherit किया जाता है उसे base class (या parent class) कहते हैं।

Key Points:

  • Code Reuse: Inherit properties and methods from existing classes
  • Hierarchy: Create a parent-child relationship between classes
  • Extensibility: Add new features to existing classes
  • Polymorphism: Enable runtime polymorphism through inheritance

Types of Inheritance

Single Inheritance

A class inherits from only one base class

Multiple Inheritance

A class inherits from multiple base classes

Multilevel Inheritance

A class inherits from a derived class

Hierarchical Inheritance

Multiple classes inherit from a single base class

Simple Example

#include <iostream>
#include <string>

// Base class
class Animal {
protected:
    std::string name;
    int age;

public:
    Animal(const std::string& n, int a) : name(n), age(a) {}
    
    void makeSound() {
        std::cout << "Some sound" << std::endl;
    }
    
    void displayInfo() {
        std::cout << "Name: " << name << ", Age: " << age << std::endl;
    }
};

// Derived class
class Dog : public Animal {
private:
    std::string breed;

public:
    Dog(const std::string& n, int a, const std::string& b) 
        : Animal(n, a), breed(b) {}
    
    void makeSound() override {
        std::cout << "Woof! Woof!" << std::endl;
    }
    
    void fetch() {
        std::cout << name << " is fetching the ball" << std::endl;
    }
    
    void displayInfo() {
        Animal::displayInfo();
        std::cout << "Breed: " << breed << std::endl;
    }
};

int main() {
    // Create a dog object
    Dog myDog("Buddy", 3, "Golden Retriever");
    
    // Call inherited and derived methods
    myDog.displayInfo();
    myDog.makeSound();
    myDog.fetch();
    
    return 0;
}

उदाहरण की व्याख्या (Example Explanation)

इस उदाहरण में Animal class से Dog class inherit करती है:

  • Base Class (Animal): name और age properties, makeSound() और displayInfo() methods
  • Derived Class (Dog): breed property, fetch() method, और overridden makeSound() method
  • Inheritance: Dog class Animal class के सभी properties और methods को inherit करती है
  • Method Overriding: Dog class makeSound() method को override करती है

Benefits of Inheritance

Inheritance के फायदे

  1. कोड का पुनः उपयोग (Code Reusability)
    • Existing code को reuse कर सकते हैं
    • Code duplication को कम करता है
  2. कोड का विस्तार (Code Extensibility)
    • नए features add कर सकते हैं
    • Existing functionality को modify कर सकते हैं
  3. कोड का संगठन (Code Organization)
    • Classes को hierarchical structure में organize करता है
    • Code को maintain करना आसान होता है

Best Practices

  • Use inheritance for "is-a" relationships
  • Keep inheritance hierarchies shallow
  • Prefer composition over inheritance when possible
  • Use protected access for base class members that need to be accessed by derived classes
  • Override virtual functions in derived classes