Hello everyone to python inheritance example. In our last tutorial, we learned about python operator overloading. In this tutorial, we are going to discuss another important object-oriented feature of python, that is Inheritance.
Table of Contents
Basically, inheritance is included in almost every object-oriented programming languages. Python Inheritance enables us to use the member attributes and methods of one class into another.
Python Inheritance Terminologies
- Superclass: The class from which attributes and methods will be inherited.
- Subclass: The class which inherits the members from superclass.
- Method Overriding: Redefining the definitions of methods in subclass which was already defined in superclass.
Python Inheritance Example
Now, let’s work with a python inheritance example program.
Copy#Line:1, definition of the superclass starts here class Person: #initializing the variables name = "" age = 0 #defining constructor def __init__(self, personName, personAge): self.name = personName self.age = personAge #defining class methods def showName(self): print(self.name) def showAge(self): print(self.age) #Line: 19, end of superclass definition #definition of subclass starts here class Student(Person): #Line: 22, Person is the superclass and Student is the subclass studentId = "" def __init__(self, studentName, studentAge, studentId): Person.__init__(self, studentName, studentAge) #Line: 26, Calling the superclass constructor and sending values of attributes. self.studentId = studentId def getId(self): return self.studentId #returns the value of student id #end of subclass definition # Create an object of the superclass person1 = Person("Richard", 23) #Line: 35 #call member methods of the objects person1.showAge() # Create an object of the subclass student1 = Student("Max", 22, "102") #Line: 39 print(student1.getId()) student1.showName() #Line: 41
Now, we are going to explain the above example to understand how inheritance works in python.
Line 1 – 19 defines the superclass. You should have no trouble in understanding this if you are familiar with python class. Class
Person is defined with necessary constructor, attributes and methods. Explanation of this segment has already been provided in Python Class tutorial.
According to the rule of inheritance, the subclass inherits the attributes and methods of its superclass. Line 22 shows how subclass Student extends Person as its superclass. The name of superclass has to be kept in parentheses while declaring the subclass. And the constructor has to call the superclass constructor with appropriate attribute values (if needed) as shown in line 26. Apart from that, everything is the same as defining a normal python class.
After defining both the superclass and the subclass, we may create objects of superclass and subclass as in lines 35 and 39. As we have been told earlier, the subclass inherits the attributes and methods. You may notice here that the object student1 (object of Student subclass) has the method showName under its scope (line 41).
After running on python compiler, the code yields the following output.
So, this was the basic of python inheritance. We shall learn more on inheritance in multiple inheritance section. Happy Coding and Good Bye!
Reference: Python.org Doc.