Site icon DataFlair

Python Program on Overloading vs Overriding

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In Python programming, the concepts of overloading and overriding play pivotal roles in object-oriented development, influencing the way methods are defined and utilized. Method overloading involves creating multiple methods with the same name but varying parameter lists within a class, offering versatility in handling different inputs.

On the other hand, method overriding occurs when a subclass provides its own implementation for a method already defined in its superclass, allowing for customization and specialization.

This article delves into the intricacies of method overloading and overriding, elucidating their distinctions and practical applications in Python.

Topic Explanation:

Method overloading in Python allows a class to define multiple methods with the same name but different parameters. This empowers developers to create versatile and adaptive functions that can accommodate various input scenarios. Python, however, does not support traditional method overloading based on the number or types of parameters, as seen in some other programming languages. Instead, developers utilize default parameter values or variable-length argument lists to achieve similar effects.

Method overriding, on the other hand, is a crucial aspect of Python’s inheritance model. When a subclass inherits from a superclass and provides its own implementation for a method present in the superclass, it overrides the inherited method. This enables developers to tailor the behavior of a method to suit the specific requirements of the subclass. Understanding the interplay between superclasses and subclasses in method overriding is fundamental to effective object-oriented design in Python.

Prerequisites:

Code 1 with Comments:

# Method Overloading
class Test:
    def addition(self, a=None, b=None, c=None, d=None):
        # Check if all parameters are provided
        if a is not None and b is not None and c is not None and d is not None:
            print(a + b + c + d)
        # Check if three parameters are provided
        elif a is not None and b is not None and c is not None and d is None:
            print(a + b + c)
        # Check if two parameters are provided
        elif a is not None and b is not None and c is None and d is None:
            print(a + b)

# Create an instance of the Test class
T = Test()

# Test method overloading with different parameter counts
print("Addition of 4 Numbers: ")
T.addition(10, 20, 30, 40)
print("Addition of 3 Numbers: ")
T.addition(10, 20, 30)
print("Addition of 2 Numbers: ")
T.addition(10, 20)

Output of Code1:

Addition of 4 Numbers:
Addition of 10, 20, 30, and 40 is 100
Addition of 3 Numbers:
Addition of 10, 20, and 30 is 60
Addition of 2 Numbers:
Addition of 10 and 20 is 30

Code 1 Explanation:

Code 2 with Comments:

# Definition of the First class
class First:
    def addition(self, a, b):
        print(a + b)

# Definition of the Second class, inheriting from First
class Second(First):
    def addition(self, a, b):
        # Calculating the sum of a and b
        c = a + b
        # Printing the addition with a formatted string
        print("Addition of %d and %d is %d" % (a, b, c))
        print("----Thanks to class me---")

# Creating an instance of the Second class
s = Second()
# Calling the addition method of the Second class
s.addition(20, 10)

Output of code 2:

Addition of 20 and 10 is 30
—-Thanks to class me—

Code 2 Explanation:

Conclusion:

In summary, exploring Python’s object-oriented paradigm underscores the importance of method overloading and overriding for creating adaptable and expressive code. Although method overloading isn’t conventionally supported in Python, developers can craft versatile functions within a class by leveraging default parameter values or variable-length argument lists. Simultaneously, method overriding shines in the context of inheritance, empowering subclasses to tailor inherited methods to meet specific requirements. When applied thoughtfully, these concepts contribute to the development of modular, reusable, and well-organized code within Python’s object-oriented landscape.

Exit mobile version