Method Overriding in OOP

Understanding runtime polymorphism through method overriding in Object-Oriented Programming

What is Method Overriding?

Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. It is a way to achieve runtime polymorphism in OOP.

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

Method Overriding एक ऐसी feature है जो subclass को अपने superclass में पहले से defined method का specific implementation provide करने की अनुमति देती है। यह OOP में runtime polymorphism को achieve करने का एक तरीका है।

Key Points:

  • Runtime Polymorphism: Method calls are resolved at runtime
  • Inheritance: Requires inheritance relationship between classes
  • Same Signature: Method must have same name, parameters, and return type
  • Virtual Function: Base class method must be virtual

Rules for Method Overriding

Rule Description
Method Signature Must match exactly (name, parameters, return type)
Access Modifier Cannot be more restrictive than base class method
Virtual Function Base class method must be virtual
Override Keyword Use override keyword to explicitly indicate overriding

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) {}
    
    // Virtual function to be overridden
    virtual void makeSound() const {
        std::cout << "Some generic animal sound" << std::endl;
    }
    
    // Virtual function with default implementation
    virtual void move() const {
        std::cout << "Animal is moving" << std::endl;
    }
    
    // Pure virtual function
    virtual void sleep() const = 0;
    
    // Non-virtual function
    void displayInfo() const {
        std::cout << "Name: " << name << ", Age: " << age << std::endl;
    }
    
    virtual ~Animal() = default;
};

// Derived class 1
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) {}
    
    // Override makeSound
    void makeSound() const override {
        std::cout << "Woof! Woof!" << std::endl;
    }
    
    // Override move
    void move() const override {
        std::cout << "Dog is running" << std::endl;
    }
    
    // Implement pure virtual function
    void sleep() const override {
        std::cout << "Dog is sleeping" << std::endl;
    }
    
    // Additional method
    void fetch() const {
        std::cout << "Dog is fetching the ball" << std::endl;
    }
};

// Derived class 2
class Cat : public Animal {
private:
    std::string color;

public:
    Cat(const std::string& n, int a, const std::string& c)
        : Animal(n, a), color(c) {}
    
    // Override makeSound
    void makeSound() const override {
        std::cout << "Meow! Meow!" << std::endl;
    }
    
    // Override move
    void move() const override {
        std::cout << "Cat is walking gracefully" << std::endl;
    }
    
    // Implement pure virtual function
    void sleep() const override {
        std::cout << "Cat is sleeping" << std::endl;
    }
    
    // Additional method
    void climb() const {
        std::cout << "Cat is climbing the tree" << std::endl;
    }
};

int main() {
    // Create objects
    Dog dog("Buddy", 3, "Golden Retriever");
    Cat cat("Whiskers", 2, "Orange");
    
    // Use base class pointer
    Animal* animal1 = &dog;
    Animal* animal2 = &cat;
    
    // Demonstrate method overriding
    std::cout << "Dog's behavior:" << std::endl;
    animal1->makeSound();  // Calls Dog's makeSound
    animal1->move();       // Calls Dog's move
    animal1->sleep();      // Calls Dog's sleep
    animal1->displayInfo(); // Calls Animal's displayInfo
    
    std::cout << "\nCat's behavior:" << std::endl;
    animal2->makeSound();  // Calls Cat's makeSound
    animal2->move();       // Calls Cat's move
    animal2->sleep();      // Calls Cat's sleep
    animal2->displayInfo(); // Calls Animal's displayInfo
    
    // Use derived class methods
    std::cout << "\nUsing derived class methods:" << std::endl;
    dog.fetch();
    cat.climb();
    
    return 0;
}

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

इस उदाहरण में method overriding का उपयोग किया गया है:

  • Base Class: Animal class में virtual और pure virtual functions हैं
  • Derived Classes: Dog और Cat classes Animal class को inherit करती हैं
  • Method Overriding: Derived classes base class के methods को override करती हैं
  • Runtime Polymorphism: Base class pointer के through derived class methods को call करते हैं

Benefits of Method Overriding

Method Overriding के फायदे

  1. Runtime Polymorphism
    • Method calls runtime पर resolve होते हैं
    • Code flexibility बढ़ती है
  2. Code Reusability
    • Base class का code reuse होता है
    • Code duplication कम होती है
  3. Extensibility
    • New functionality add करना आसान होता है
    • Existing code को modify किए बिना extend कर सकते हैं

Best Practices

  • Always use virtual destructors in base classes
  • Use override keyword to explicitly indicate overriding
  • Keep method signatures consistent across inheritance hierarchy
  • Document the behavior of overridden methods
  • Consider using pure virtual functions for abstract classes