- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Method Overloading and Method Overriding in Python
Methods are important when it comes to Python's object-oriented programming (OOP). They are crucial for isolating functionality within a class and giving things the ability to carry out particular functions. However, two OOP concepts—method overloading and method overriding—can occasionally cause misunderstanding. The distinctions between these two ideas and their applications in Python will be discussed in this article.
Using the "def" verb, the method's name, arguments, and body are all required to define a method in Python.
def method_name(parameter1, parameter2, ...): # method body return value
Refers to defining multiple methods with the same name but different parameters
Refers to defining a method in a subclass that has the same name as the one in its superclass
Can be achieved in Python using default arguments
Can be achieved by defining a method in a subclass with the same name as the one in its superclass
Allows a class to have multiple methods with the same name but different behaviors based on the input parameters
Allows a subclass to provide its own implementation of a method defined in its superclass
The choice of which method to call is determined at compile-time based on the number and types of arguments passed to the method
The choice of which method to call is determined at runtime based on the actual object being referred to
Not supported natively in Python
Supported natively in Python
Example 1: Method Overloading
Method overloading refers to defining multiple methods with the same name but with different parameter types or the number of parameters. In Python, method overloading is achieved using default arguments.
class MyClass: def my_method(self, arg1, arg2=None): if arg2: print(arg1 + arg2) else: print(arg1) # Calling the method with one argument obj = MyClass() obj.my_method(10) # Calling the method with two arguments obj.my_method(10, 20)
We defined a method called "my_method" in this example that accepts two arguments, the second of which is optional. The method will add the two arguments if there is a second argument; Otherwise, the first argument will be printed.
Example 2: Method Overriding
Method overriding, on the other hand, refers to defining a method in a subclass with the same name as the one in its superclass. The subclass method then overrides the superclass method.
class Animal: def speak(self): print("Animal Speaking!") class Dog(Animal): def speak(self): print("Dog barking!") obj = Dog() obj.speak()
We have a "Dog" subtype and a "Animal" superclass as seen above. The "speak" method of the "Animal" class is overridden by the "Dog" class's "speak" function. Now, let's look at a complete block of Python code with method overloading and method overriding −
class Parent: def my_method(self, arg1, arg2): print("Parent my_method") class Child(Parent): def my_method(self, arg1): print("Child my_method") obj = Child() obj.my_method("Hello")
We have a superclass named "Parent" in this case as well as a subclass called "Child." The "Child" class has a method named "my method" that only accepts one parameter, whereas the "Parent" class's "my method" method accepts two arguments. The "my method" method is invoked when it is called on a "Child" class instance because it only accepts one argument.
class Shape: def area(self): pass class Rectangle(Shape): def __init__(self, length, breadth): self.length = length self.breadth = breadth def area(self): return self.length * self.breadth class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius * self.radius def area(self, diameter): self.radius = diameter / 2 return 3.14 * self.radius * self.radius r = Rectangle(5, 10) print("Area of rectangle:", r.area()) c = Circle(7) print("Area of circle with radius 7:", c.area()) print("Area of circle with diameter 10:", c.area(10))
Area of rectangle: 50 Area of circle with radius 7: 153.86 Area of circle with diameter 10: 78.5
Here, both the Circle and the Rectangle modify the area() method of the basic Shape class. While the Circle class overrides the area() method to compute the area of a circle with a specified radius, the Rectangle class defines its own version of the area() method to calculate the area of a rectangle. By adding a second area() function that determines the area of a circle with a specified diameter, the Circle class also demonstrates method overloading. In this case, the superclass "Father" and the subclass "Child" are both present. While the "Child" class only takes one argument, the "my method" method for the "Parent" class admits two.
The rectangular has 50 square feet when an instance of the Rectangle class is created and its area() method is used. The area() function of the Circle class returns the circle's area, which is approximately 153.86 when we use an instance with a radius of 7. Last but not least, we use a circle with a diameter of 10 and call the area() function of the Circle class to find the area of the circle, which is approximately 78.5.
Understanding the subtle differences between method overloading and method calling is essential when exploring the world of object-oriented programming. The two are frequently confused, which can cause problems and uncertainty in the future. The technique of specifying numerous methods with the same name but distinct parameters is known as method overloading. This contrasts with method overriding, which entails creating a method with the same name as one in the superclass in a subtype. To override a method in Python, a method with the same name as the one in the superclass must be provided. Preset numbers are frequently used as a fix for function overloading.
Kickstart Your Career
Get certified by completing the courseGet Started