Method overloading and method overriding are two important terms in object-oriented programming. You might find these two terms confusing, but it won’t be anymore. In this article, let’s understand the differences between method overloading and method overriding.
What is the difference between method overloading and method overriding? Method overloading allows multiple methods in the same class to have the same name but different parameters. Method overriding allows a parent class and a child class to have methods with the same name and same parameters. The child class method will override the parent class method.
Below is a table that points out the differences between method overloading and method overriding.
|Method Overloading||Method Overriding|
|Method with same name but different number of arguments||Method with same name and same number of arguments|
|Inheritance is optional||Inheritance required|
|Takes place in methods within a class||Methods reside in different classes|
|Can be done within a class||At least 2 classes are required|
|Binding of overloaded methods is done at compile-time; hence it is part of compile-time polymorphism||Binding of overridden methods is done at run-time; hence it is part of run-time polymorphism|
|Static method can be overloaded||Static methods cannot be overridden|
|Increases code reusability||Used in implementation of specific scenarios|
|Relates with Polymorphism||Relates with Inheritance|
Now let’s dive deep into both overloading and overriding and understand the concepts clearly with some simple examples.
To understand method overloading and overriding, you must know the concept of polymorphism. Poly means many. Morphism means forms. So, polymorphism means “many forms”. In programming terms, it means that there can be many functions with the same name but different forms, arguments, or operations.
An Example of Polymorphism in Python:
print(max(10,20)) print(max([100,20,90,40,50,70])) print(max('A','S','Z','D','V')) print(max(10.51,2.20,32.54,87.32,90.12,15.63,34.65))
We use the same method every time, i.e., max(), which performs the same function every time, i.e., to find the maximum value. But the number and types of arguments are different every time.
- We used max() with individual values and lists.
- We used max() with a various number of arguments.
- We used max() with integer, character, and float data types.
The max() is an example where the method has the same name but different implementations.
Now that you know what polymorphism is, the concept of method overloading is easy to understand.
As the name suggests, we are overloading the methods, which means there would be many methods with the same name within a class, but having a different number of arguments.
class OverloadingExample: def add(self,a,b): print(a+b) def add(self,a,b,c): print(a+b+c) a=OverloadingExample() a.add(5,10) a.add(5,10,20)
Note: Python does not support method overloading, this is because python always picks up the latest defined method. We can still overload methods in python but it would be of no use. However, you can implement method overloading in the above way in Java, C++, etc.
An alternative to performing method overloading in Python would be in this way:
class OverloadingExample: def add(self, x = None, y = None): if x != None and y != None: return x + y elif x != None: return x else: return 0 obj = OverloadingExample() print("Value:", obj.add()) print("Value:", obj.add(4)) print("Value:", obj.add(10,20))
Here, we don’t mention the function definition multiple times. Instead, we manipulate the arguments using the same method repeatedly.
In method overriding, we will have many methods, and this time the number of arguments will also be the same.
Now you must be wondering how these methods are different from each other if their name and signatures are the same. So, the answer is that they differ in location. Methods will be placed in different classes.
class Class1: def display(self): print("Hello from Class1") class Class2: def display(self): print("Hello from Class2") c1 = Class1() c2 = Class2() c1.display() c2.display()
Now let’s connect both the classes using Inheritance.
In Inheritance, as the name suggests, one class inherits the properties of another class. Let’s understand inheritance with the help of a real-world example:
Son inherits all the features from his Father but has his unique features too. Here, the Father is the parent class (also called base class, super class, etc.), and Son is the child class (also called derived class).
class Father: def sleep(self): print("Sleep") def eat(self): print("Eat") def blackHair(self): print("Black Hair") class Son(Father): def cry(self): print("Cry") def study(self): print("Study") sonObject = Son() sonObject.sleep() sonObject.eat() sonObject.blackHair() sonObject.cry() sonObject.study()
You can see that the object of the Son class can access the methods of the Father class, along with accessing its own methods.
Now since we know how inheritance works, let’s see what happens when we do method overriding.
class Class1: def display(self): print("Hello from Class1") class Class2(Class1): def display(self): print("Hello from Class2") c2 = Class2() c2.display()
Here, Class2 inherits Class1. Hence, the objects of Class2 should be able to access the methods of Class1. But since the method names are the same in both classes, the method in the child class overrides the method in the parent class.
So, if you want to access overridden methods inside the child class, you need to use the super() method.
class Class1: def display(self): print("Hello from Class1") class Class2(Class1): def display(self): print("Hello from Class2") super().display() c2 = Class2() c2.display()
In this article, we have seen the fundamental concepts of method overloading and method overriding along with example Python programs.
I hope these concepts are clear to you. Now you can implement these concepts while you do object-oriented programming.
Check out this article if you want to understand the differences between method overloading and operator overloading.