Tutorial 9 – Overriding, Overloading in Python
Welcome to the ninth tutorial of Selenium-Python series. In the previous tutorial we had studied about inheritance. In this tutorial, we will learn about overriding, overloading in python.What you will Learn:
- What is Overriding?
- Function Overloading
- Overload methods using latest defined method
What is Overriding?
Let’s suppose we have a parent ‘Car’ class having a function ‘transmission’. This function defines that the ‘Car’ class has manual shift gears. We have another class ‘Toyota’ that inherits ‘Car’ class (see line 5). So the ‘Toyota’ class automatically inherits the ‘tranmission’ function of ‘Car’ class. However, the ‘Toyota’ car manufacturers decide that the ‘Toyota’ car should have automatic shift gears instead of manual. So the manufacturers decide to ‘Override’ the ‘transmission’ function (see lines 6,7). So this is what ‘function overriding’ is. A child class has a function that has the same name but with a different implementation.
Figure 1: Function Overriding
So when we create an object of ‘Toyota’ class & call the ‘transmission’ function, the overrided ‘transmission’ function of ‘Toyota’ class will get invoked. Hence ‘Automatic transmission’ gets printed in console, see below
Figure 2: Function Overriding
Similarly see below. We have a ‘StudentDetails’ child class that inherits ‘Study’ class (line 6). The child class has an overrided function ‘showMarks’ that returns ‘marks + 20’ . So when we create object of ‘StudentDetails’ class & call the ‘showMarks’ function, it invokes the overrided ‘showMarks’ function & prints 120 (which is 100 + 20)
Figure 3: Function Overriding
However if we remove the showMarks function from child class than 100 gets printed, as expected, see below (due to inheritance)
If the child class does not inherit ‘Study’ class as seen below, than we will get an error at line number 10
Coming back to overriding, look at below program. The ‘StudentDetails’ child class inherits 2 parent classes: Study & Sports. The parent class has ‘showMarks’ function (line 2) that returns marks (line 4). This function is also present in child class (line 14) that returns ‘marks + 30’ (line 16). We create an object of child class & call this overriding function ‘showMarks’ (line 19). When we run this program, ‘230’ gets printed in console which is self-explanatory.
Figure 6: Function Overriding
Similarly look at below program. We have added overriding function ‘showSports’ (line 17) that returns line 19. When we run the program ‘hockey is national game’ gets printed as expected
Figure 7: Function Overriding
See below. We have defined 2 methods in the ‘Study’ class. Both the methods have the same name ‘showMarks’. The only difference is in the number of parameters that they define. In the line 2, there are 2 parameters: self & marks. In line 5, we have 3 parameters: self, marks & percentage. This concept is known as function overloading. Notice that there is an error in line number 5
Figure 8: Function Overloading
If you hover over the error, you notice ‘Duplicated signature: showMarks’ error
Figure 9: Function Overloading
So what this means is that, we cannot have the functions having the same name inside a single class. So method overloading is not possible in python. However see below. We create a class with one method sayHello(). The first parameter of this method is set to ‘None’, this gives us the option to call it with or without a parameter.
An object is created and we call ‘sayHello’ method using zero (line 10) and one parameter (line 11). We see different output for both of these.
Figure 10: Function Overloading
Overload methods using latest defined method
So like other languages (for example method overloading in C++) do, python does NOT support method overloading. We may overload the methods but can only use the latest defined method, see below. Notice that ‘48’ is printed in console.
Notice that, in-spite of duplicate signature error in line 9 above, we are still able to run our program.
Now see below. If you uncomment line 14, you get an error.
Now see below. Comment line 17 (method having 3 parameters) & uncomment line 14 (method having 2 parameters), run program. Why do we still get an error? Why wasn’t product(a, b) viz line number 3 called automatically with 2 parameters? This is because the latest defined method has 3 parameters viz line number 9
Now swap the position of methods, see below. Now the latest method definition (line 9) has 2 parameters. So now there will not be any error if we try to call line 13 (having 2 parameters).
In this tutorial we have studied about overriding & overloading in python. Thank you for reading!