Difference between Overloading and Overriding in Python
Introduction
Python is an object-oriented programming language that supports both overloading and overriding of methods. These concepts play a crucial role in achieving code reusability and flexibility in programming. However, beginners may find it challenging to differentiate between overloading and overriding, as both involve modifying existing methods. In this article, we will explore the differences between overloading and overriding in Python and provide examples to help you understand their usage.
Method Overloading in Python
Method overloading is a technique that enables a class to have numerous methods with the same name but distinct parameters or argument lists. The quantity, arrangement, and kind of inputs given at runtime all affect how the procedure is implemented. Although method overloading is not expressly supported in Python, we can create it via operator overloading, default parameters, and variable-length arguments.
Default Arguments
You can define default values for function parameters in Python. Without a specific parameter, the default value will be applied if the function is invoked.
Example:
Here's an example of how to use default arguments to achieve method overloading:
class Rectangle: def __init__(self, length=0, breadth=0): self.length = length self.breadth = breadth def area(self): returnself.length * self.breadth def perimeter(self): return 2 * (self.length + self.breadth) # Creating objects and calling methods rect1 = Rectangle(10, 20) rect2 = Rectangle(10) rect3 = Rectangle() print("Area of rectangle 1:", rect1.area()) print("Perimeter of rectangle 1:", rect1.perimeter()) print("Area of rectangle 2:", rect2.area()) print("Perimeter of rectangle 2:", rect2.perimeter()) print("Area of rectangle 3:", rect3.area()) print("Perimeter of rectangle 3:", rect3.perimeter())
Output:
Area of rectangle 1: 200 Perimeter of rectangle 1: 60 Area of rectangle 2: 0 Perimeter of rectangle 2: 20 Area of rectangle 3: 0 Perimeter of rectangle 3: 0
The Rectangle class in the example above includes two methods that determine a rectangle's area and perimeter. The length and breadth inputs that are taken by the __init__ method are used to initialise the object's attributes. In the absence of any arguments, the default values of 0 will be applied. The appropriate methods will be called in accordance with the number of arguments used to create the object.
Variable-length Arguments
Another way to achieve method overloading in Python is by using variable-length arguments. The special symbols *args and **kwargs in Python are used to pass arguments of variable length. You can give a variable number of non-keyword arguments to a function using the *args parameter, whereas you can pass a variable number of keyword arguments using **kwargs.
Example:
class Addition: def add(self, *args): total = 0 fori in args: total += i return total # Creating objects and calling methods add1 = Addition() print("Sum of numbers 1, 2, 3:", add1.add(1, 2, 3)) print("Sum of numbers 1, 2, 3, 4, 5:", add1.add(1, 2, 3, 4, 5))
Output:
Sum of numbers 1, 2, 3: 6 Sum of numbers 1, 2, 3, 4, 5: 15
The Addition class in the example above includes a method called add that accepts arguments of various lengths via the *args parameter. All of the inputs supplied to the method are added up. We can build objects and use a different amount of arguments when calling the add method.
Operator Overloading
In Python, you can utilise operator overloading to work with objects from classes that you specify. When an operator is applied to a class object, special methods that you create are invoked. For instance, if you define the __add__ method, you can add two objects belonging to your class using the + operator.
Here is an example:
Example:
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y) # Creating objects and calling methods p1 = Point(1, 2) p2 = Point(3, 4) p3 = p1 + p2 print("p1: ({}, {})".format(p1.x, p1.y)) print("p2: ({}, {})".format(p2.x, p2.y)) print("p3: ({}, {})".format(p3.x, p3.y))
Output:
p1: (1, 2) p2: (3, 4) p3: (4, 6)
The __add__ function, which is invoked when the + operator is used with two objects of the Point class, is defined by the Point class in the above mentioned example. The method adds the x and y coordinates of the two items supplied as arguments to generate a new Point object.
Method Overriding in Python
A subclass can provide a distinct implementation of a method that is already specified in its superclass by using the technique of method overriding. The method can be inherited by the subclass from the superclass and modified to suit its requirements. When you wish to give a method in a subclass a more specialised implementation, method overriding can be handy.
Example:
class Animal: defmake_sound(self): print("Generic animal sound") class Cat(Animal): defmake_sound(self): print("Meow") class Dog(Animal): defmake_sound(self): print("Woof") # Creating objects and calling methods a1 = Animal() c1 = Cat() d1 = Dog() a1.make_sound() c1.make_sound() d1.make_sound()
Output:
Generic animal sound Meow Woof
A generic animal sound is printed by the Animal class's make sound method in the example above. The make sound method is overridden by the Cat and Dog classes, which both derive from the Animal class. Different noises are produced when the make sound method is called on objects belonging to the Animal, Cat, and Dog classes.