OOP/OOD  

Difference Between Method Overriding and Method Overloading in Python?

🔄 What is Method Overriding?

Method overriding means that a child class (subclass) gives its own version of a method that already exists in the parent class (superclass). The method name and its parameters remain the same, but the child class changes the behavior.

This is useful when the child class needs a different action than what the parent class provides.

âś… Example of Method Overriding in Python:

class Animal:
    def speak(self):
        print("Animal makes a sound")

class Dog(Animal):
    def speak(self):  # Overriding parent class method
        print("Dog barks")

# Usage
a = Animal()
a.speak()  
# Output: Animal makes a sound

d = Dog()
d.speak()  
# Output: Dog barks

👉 In this example, the Animal class has a method called speak(). The Dog class changes it by writing its own version of speak(). This is method overriding.

🔀 What is Method Overloading?

In some languages like Java or C++, you can write multiple methods with the same name but different parameters. This is called method overloading. But in Python, if you write methods with the same name, the last one overwrites the previous ones.

However, we can still imitate overloading in Python using techniques like default arguments or *args and **kwargs.

âś… Example of Method Overloading using Default Arguments:

class Math:
    def add(self, a=0, b=0, c=0):
        return a + b + c

m = Math()
print(m.add(2, 3))       
# Output: 5

print(m.add(2, 3, 4))    
# Output: 9

print(m.add())           
# Output: 0

👉 Here, even though the method name is the same (add), it can work with 0, 2, or 3 numbers because we used default values.

âś… Example of Method Overloading using *args:

class Calculator:
    def multiply(self, *args):
        result = 1
        for num in args:
            result *= num
        return result

c = Calculator()
print(c.multiply(2, 3))        
# Output: 6

print(c.multiply(2, 3, 4))     
# Output: 24

👉 In this case, the method multiply can take any number of inputs and multiply them together. This makes it act like it is overloaded.

🔍 Key Differences Between Method Overriding and Method Overloading

Method Overriding

  • It happens when a child class changes the behavior of a method that is already present in the parent class.

  • The method name and its arguments remain the same as the parent class.

  • It always requires inheritance.

  • It is mostly used when we want the child class to act differently than the parent class.

Method Overloading

  • In Python, we don’t have traditional method overloading like in Java or C++. Instead, we achieve it with default parameters or variable-length arguments.

  • It means writing one method that can handle different numbers of inputs.

  • In Python, the method name stays the same, but the behavior changes depending on how many arguments we pass.

  • It does not require inheritance.

📌 Summary

Method overriding and method overloading both make programming in Python more flexible, but they are different concepts. Overriding means that a child class provides its own version of a parent class method, which is very useful in inheritance and polymorphism. On the other hand, overloading in Python is not directly supported but can be simulated using default arguments or variable-length arguments, allowing one method to work in many different ways. In simple words, overriding changes behavior across different classes, while overloading changes behavior inside the same class. 🚀