Introduction to Python Inheritance

If you are interested to learn about the Python classes and objects

What is Python Inheritance?

Inheritance allows us to define a class that inherits all the methods and properties from another class. It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.

Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

Python Inheritance Syntax

 lass BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class

Derived class inherits features from the base class where new features can be added to it. This results in re-usability of code.

Example of Inheritance in Python

To demonstrate the use of inheritance, let us take an example. A polygon is a closed figure with 3 or more sides. Say, we have a class called Polygon defined as follows.

class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    def dispSides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])

This class has data attributes to store the number of sides n and magnitude of each side as a list called sides. The inputSides() method takes in the magnitude of each side and dispSides() displays these side lengths. A triangle is a polygon with 3 sides. So, we can create a class called Triangle which inherits from Polygon. This makes all the attributes of Polygon class available to the Triangle class. We don’t need to define them again (code reusability). Triangle can be defined as follows.

class Triangle(Polygon):
    def __init__(self):
        Polygon.__init__(self,3)

    def findArea(self):
        a, b, c = self.sides
        # calculate the semi-perimeter
        s = (a + b + c) / 2
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
        print('The area of the triangle is %0.2f' %area)

However, class Triangle has a new method findArea() to find and print the area of the triangle. Here is a sample run.

>>> t = Triangle()

>>> t.inputSides()
Enter side 1 : 3
Enter side 2 : 5
Enter side 3 : 4

>>> t.dispSides()
Side 1 is 3.0
Side 2 is 5.0
Side 3 is 4.0

>>> t.findArea()
The area of the triangle is 6.00

We can see that even though we did not define methods like inputSides() or dispSides() for class Triangle separately, we were able to use them. If an attribute is not found in the class itself, the search continues to the base class. This repeats recursively, if the base class is itself derived from other classes.

Create a Parent Class

Any class can be a parent class, so the syntax is the same as creating any other class:

Example

Create a class named Person, with firstname and lastname properties, and a printname method:

class Person:<br>  def __init__(self, fname, lname):<br>  
  self.firstname = fname<br>    self.lastname = lname
def printname(self):
    print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")<br>x.printname()

Create a Child Class

To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:

Example

Create a class named Student, which will inherit the properties and methods from the Person class:

class Student(Person):<br>  pass

Note: Use the pass keyword when you do not want to add any other properties or methods to the class. Now the Student class has the same properties and methods as the Person class.

Example

Use the Student class to create an object, and then execute the printname method:

x = Student("Mike", "Olsen")<br>x.printname()

Add the __init__() Function

So far we have created a child class that inherits the properties and methods from its parent. We want to add the __init__() function to the child class (instead of the pass keyword).

Note: The __init__() function is called automatically every time the class is being used to create a new object.

Example

Add the __init__() function to the Student class:

class Student(Person):
<br>  def __init__(self, fname, lname):
<br>  #add properties etc.

When you add the __init__() function, the child class will no longer inherit the parent’s __init__() function.

Note: The child’s __init__() function overrides the inheritance of the parent’s __init__() function.

To keep the inheritance of the parent’s __init__() function, add a call to the parent’s __init__() function:

Example

class Student(Person):
<br>  def __init__(self, fname, lname):
<br>    Person.__init__(self, fname, lname)

Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__() function.

Use the super() Function

Python also has a super() function that will make the child class inherit all the methods and properties from its parent:

Example

class Student(Person):
<br>def __init__(self, fname, lname):
<br> super().__init__(fname, lname)

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.

Add Properties

Example

Add a property called graduationyear to the Student class:

class Student(Person):<br>  def __init__(self, fname, lname):<br>    super().__init__(fname, lname)<br>    self.graduationyear = 2019

In the example below, the year 2019 should be a variable, and passed into the Student class when creating student objects. To do so, add another parameter in the __init__() function:

Example

Add a year parameter, and pass the correct year when creating objects:

class Student(Person):
<br>  def __init__(self, fname, lname, year):
<br>    super().__init__(fname, lname)<br>    self.graduationyear = year
x = Student("Mike", "Olsen", 2019)

Add Methods

Example

Add a method called welcome to the Student class:

class Student(Person):
<br>def __init__(self, fname, lname, year):
<br>super().__init__(fname, lname)<br>self.graduationyear = year
def welcome(self):
  print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.

There are five types of inheritances:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

Single Inheritance

This type of inheritance enables a subclass or derived class to inherit properties and characteristics of the parent class, this avoids duplication of code and improves code reusability.

#parent class
class Above:
    i = 5
    def fun1(self):
        print(“Hey there, you are in the parent class”)

#subclass
class Below(Above):
    i=10
    def fun2(self):
        print(“Hey there, you are in the sub class”)

temp1=Below()
temp2=Above()
temp1.fun1()
temp1.fun2()
temp2.fun1()
print(temp1.i)
print(temp2.i)
#temp2.fun2()

Alright, let’s walk through the above code.

In the above code “Above” is the parent class and “Below” is the child class that inherits the parent class. Implementing inheritance in python is a simple task, we just need to mention the parent class name in the parentheses of the child class. We are creating objects of both parent class and child class, and here comes an interesting point about the inheritance. A child class can access the methods and variables of the parent class, whereas the vice versa is not true.

So in the above code temp1 object can access both fun1 and fun2 methods whereas the temp2 object can access only the fun1 method. Similarly, the same rule applies to variables in the code. And accessing a child class method or variable from a parent class object will throw an error. If the last line in the code is uncommented then it raises an error.

Multiple Inheritance

This inheritance enables a child class to inherit from more than one parent class. This type of inheritance is not supported by java classes, but python does support this kind of inheritance. It has a massive advantage if we have a requirement of gathering multiple characteristics from different classes.

#parent class 1
class A:
    demo1=0
    def fun1(self):
        print(self.demo1)

#parent class 2
class B:
    demo2=0
    def fun2(self):
        print(self.demo2)

#child class
class C(A, B):
    def fun3(self):
        print(“Hey there, you are in the child class”)

# Main code
c = C()
c.demo1 = 10
c.demo2 = 5
c.fun3()
print(“first number is : “,c.demo1)
print(“second number is : “,c.demo2)

In the above code, we’ve created two parent classes “A”, “B”. Following the syntax of the inheritance in python, we’ve created a child class, which inherits both classes “A” and “B”. As discussed earlier that a child class can access the methods and variables of the parent class, The child class “C” can access the methods of its parent class.

Multilevel Inheritance

In multilevel inheritance, the transfer of the properties of characteristics is done to more than one class hierarchically. To get a better visualization we can consider it as an ancestor to grandchildren relation or a root to leaf in a tree with more than one level.

#parent class 1
class vehicle:
    def functioning(self):
        print(“vehicles are used for transportation”)

#child class 1
class car(vehicle):
    def wheels(self):
        print(“a car has 4 wheels”)
       
#child class 2
class electric_car(car):
    def speciality(self):
        print(“electric car runs on electricity”)

electric=electric_car()
electric.speciality()
electric.wheels()
electric.functioning()

Having a dry run over the above code, we’ve created a class “vehicle”, then we’ve created a class car that inherits the class vehicle. Now the “vehicle” is a parent class and the “car” is a child class. Later we’ve created an “electric_car” class, now the car class is a parent class and the electric_car class is a child class, and the relationship between vehicle class and electric_car class is the multilevel inheritance.

Here electric_car class can access the methods, variables of both vehicle and car class, whereas car class can access only the methods, variables of vehicle class. And as discussed parent class vehicle cannot access any method of the child class.

Hierarchical Inheritance

This inheritance allows a class to host as a parent class for more than one child class or subclass. This provides a benefit of sharing the functioning of methods with multiple child classes, hence avoiding code duplication.

#parent class
class Parent:
    def fun1(self):
        print(“Hey there, you are in the parent class”)
 
#child class 1
class child1(Parent):
    def fun2(self):
        print(“Hey there, you are in the child class 1”)

#child class 2 
class child2(Parent):
    def fun3(self):
        print(“Hey there, you are in the child class 2”)
 
#child class 3
class child3(Parent):
    def fun4(self):
        print(“Hey there, you are in the child class 3”)
 
# main program
child_obj1 = child3()
child_obj2 = child2()
child_obj3 = child1()
child_obj1.fun1()
child_obj1.fun4()
child_obj2.fun1()
child_obj2.fun3()
child_obj3.fun1()
child_obj3.fun2()

In the above code, we have a single parent class and multiple child classes inheriting the same parent class. Now all the child classes can access the methods and variables of the parent class. We’ve created a “Parent” class and 3 child classes “child1”, “child2”, “child3”, which inherits the same parent class “Parent”.

Checkout:  Python Open Source Project Ideas

Hybrid Inheritance

An inheritance is said hybrid inheritance if more than one type of inheritance is implemented in the same code. This feature enables the user to utilize the feature of inheritance at its best. This satisfies the requirement of implementing a code that needs multiple inheritances in implementation.

class A:
def fun1(self):
print(“Hey there, you are in class A”)class B(A):
def fun2(self):
print(“Hey there, you are in class B”)class C(A):
def fun3(self):
print(“Hey there, you are in class C”)class D(C,A): #line 13
def fun4(self):
print(“Hey there, you are in the class D”)#main program
ref = D()
ref.fun4()
ref.fun3()
ref.fun1()

In the above code, we can see that we’ve implemented more than one type of inheritance. Classes A, B, C implements hierarchical inheritance, and classes A, C, D implements multilevel inheritance. Noe those individual inheritances have their individual properties of accessing methods and variables of the parent class. Also, there’s a point to be noted.

When we are implementing multilevel inheritance we follow syntax like “child_class(parent_class1, parent_class2)”. But this syntax will throw an error if “parent_class1” is hierarchically above the “parent_class2”. If we want to implement this syntax, then the “parent_class1” must be in a hierarchically lower level than “parent_class2”. For example in the above code, if line 13 has a syntax class D(A, C) then the code wouldn’t work since class C is hierarchically lower than class A.

Introduction to Python Inheritance
Show Buttons
Hide Buttons