Python Inheritance Example

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.

Python Inheritance

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

  1. Superclass:
    The class from which attributes and methods will be inherited.
  2. Subclass:
    The class which inherits the members from superclass.
  3. 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.

#Line:1, definition of the superclass starts here  
class Person:  
    #initializing the variables  
    name = ""  
    age = 0  
    #defining constructor  
    def __init__(self, personName, personAge):  = personName  
        self.age = personAge  
    #defining class methods  
    def showName(self):  
    def showAge(self):  
    #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  
# Create an object of the subclass  
student1 = Student("Max", 22, "102")  #Line: 39
student1.showName()  #Line: 41

Now, we are going to explain the above example to understand how inheritance works in python.

Defining Superclass

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.

Defining Subclass

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.

Python Inheritance Example

So, this was the basic of python inheritance. We shall learn more on inheritance in multiple inheritance section. Happy Coding and Good Bye!

You can checkout complete python script and more Python examples from our
GitHub Repository

Reference: Doc.

Leave a Reply

Your email address will not be published. Required fields are marked *