Tutorial 8 – Inheritance in Python
Welcome to the eighth tutorial of Selenium-Python series. In our last tutorial we studied about exception handling in python. In this tutorial, we will study about inheritance concept in python.
- What you will Learn:
- What is inheritance?
- Types of inheritance
- Single level inheritance
- Object as a super class
- Multi level inheritance
- Multiple inheritance
What is inheritance?
To understand inheritance, let us create a simple class ‘Animal’. Now ‘Animal’ can perform different functionalities like it can move, sleep, run etc. Let us define 3 functions over here, see below. Remember that ‘self’ keyword is used to access the attributes & methods of a function.
Let us create an object of ‘Animal’ class by creating an instance of the class, see below. This object will now point towards the functions of ‘Animal’ class & we can call the functions, see lines 12-14 below. The console o/p shows the respective function’s output.
Now suppose we have different animals. Let us say we have another animal ‘dog’. So lets create a class ‘dog’, see below. Let it have the same 3 functions viz eat, sleep, move. Lets create object of ‘dog’ class & than call these functions. This would print the same o/p viz Sleeping, Eating, Moving.
Now let’s say we have 5 other animals, do you think that we have to write the same set of functions again and again? Also notice that they are performing the same functions viz sleeping, eaiting, moving. The answer is ‘NO’. Now ‘dog’ is a ‘Animal’. So every function present in ‘Animal’ class should be inherited to the ‘dog’ class as well. Similalrly ‘cat’ is a ‘Animal’. So every function present in ‘Animal’ class should be inherited to the ‘cat’ class. So there is a ‘is a’ relationship in inheritance. So, Maruti is a Car, Samsung is a Phone, Venus is a Planet and so on.. See below. So instead of saying ‘class dog’, we can say ‘class dog(Animal)’ . So ‘dog’ class will inherit all the properties of ‘Animal’ class. We can now remove all the functions inside the ‘dog’ class & can simply write ‘pass’. When run the program, you notice that the console o/p is still the same.
Thus we use inheritance for code re-usability.
Types of inheritance
Inheritance is of 3 types:
a) Single level inheritance
b) Multi level inheritance
c) Multiple inheritance
Single level inheritance
See below. We have created a class ‘Person’ having a function ‘PersonDetails’. This function has 2 instance variables: firstName & lastName, that are initialized in lines 3 &4. We than create another class ‘Employee’ that inherits ‘Person’ class (line 6). So ‘Employee’ class will inherit all the functions of ‘Person’ class. Let this ‘Employee’ class has its own function ‘EmployeeDetails’. This function initializes the ID of employee. The Employee class has one more function ‘show’ that will print the values of all the 3 instance variables: firstName, lastName and employeeID.
Figure 5: Single level inheritance
See below figure. All the properties of parent class ‘Person’ will be inherited by child class ‘Employee’ but vice versa is not true.
Figure 6: Single level inheritance
Now to call the functions, we have to make an object of child class ‘Employee’, see below figure. Now, why did we make an object of child class ‘Employee’ and not the parent class ‘Person’ ?? The reason is, the child class ‘Employee’ is inheriting all the properties of ‘Person’ class
Figure 7: Single level inheritance
See below. As soon as you type obj. you will see all the functions & variable names that can be accessed
Figure 8: Single level inheritance
See below. Let us call all the 3 functions (lines 16, 17, 18)
Figure 9: Single level inheritance
Enter firstName, lastName & employeeID as seen below in lines 16-18.
Figure 10: Single level inheritance
Run program, we see the o/p in console as ‘Test User 24’. So this is ‘Single level inheritance’
Figure 11: Single level inheritance
Object as a super class
‘object’ is the superclass (parent) of all the classes, see line#1 below. This simply means that ‘Person’ class inherits the ‘object’ class. Please note that, whether you write ‘Person’ or ‘Person(object)’, any class will inherit the ‘object’ class. To understand the concept, we have explicitly shown as ‘Person(object)’, however we don’t have to write like this every time, since behind the scenes every class inherits ‘object’ class. When we run program, the o/p s still the same.
Multi level inheritance
See below figure. We have created 3 classes: ‘SuperClass’ is the parent class, ‘SubClass’ inherits ‘SuperClass’ and ‘AnotherSubClass’ inherits ‘SubClass’. We have also defined the functions in each class
Figure 13: Multi-level inheritance
See figure below. ‘AnotherSubClass’ inherits properties of super class & subclass. The subclass inherits properties of only super class. However, the superclass does not inherit anything from subclass & anothersubclass. This is multi-level inheritance.
Figure 14: Multi-level inheritance
So the advantage of multi level inheritance is that we have to create the object of only ‘AnotherSubClass’ since that inherits both subclass as well as super class. See figure 15. When we run the program, the console o/p shows 100, 102 (viz 100+2), 103 (viz 100+3). So this is all about multi-level inheritance.
Figure 15: Multi-level inheritance
In multiple inheritance, a class can inherit from more than one class. See figures 16 & 17. The ‘StudentDetails’ class inherits the features of 2 classes: Study & Sports. You can consider ‘Study’ & ‘Sports’ classes to represent father and mother & ‘StudentDetails’ class as their child.
Figure 16: Multiple Inheritance
Figure 17: Multiple Inheritance
We can now create an object of ‘StudentDetails’ class & call the 3 functions, see below (lines 17-20)
Figure 18: Multiple Inheritance
So let us enter the name, age, marks & sports, see below. See the o/p in console
Figure 19: Multiple Inheritance
In this tutorial we studied about inheritance in python. Thank you for reading!