Inheritance Python

Inheritance in Python: Types

Inheritance in Python: Types

Introduction

Inheritence is a process of obtaining properties and characteristics(variables and methods) of another class. In this hierarchical order, the class which inherits another class is called subclass or child class, and the other class is the parent class.

Inheritance is categorized based on the hierarchy followed and the number of parent classes and subclasses involved.

  • It is an excellent representation of relationships in the real world.
  • It allows code reuse. It doesn't require us to create the same code repeatedly and again. It also allows us to add options to an existing class without having to modify the existing code.
  • It is a transitive nature, meaning that if B is inherited from another class A, all the subclasses belonging to B will inherit directly from class A.

Types of Inheritance

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()

Output:

Hey there, you are in the parent class
Hey there, you are in the sub class
Hey there, you are in the parent class
10
5

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)

Output:

Hey there, you are in the child class
first number is10
second number is5

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

Multilevel inheritance

The features that are part of the original class, as well as the class that is derived from it, are passed on to the new class. It is similar to a relationship involving grandparents and children.

Types of inheritance Python

Example:

# Python program for demonstrating multilevel inheritance 
 
# Here, we will create the Base class  
class Grandfather1
 
    def __init__(self, grandfathername1): 
        self.grandfathername1 = grandfathername1 
 
# here, we will create the Intermediate class 
class Father1(Grandfather1): 
    def __init__(self, fathername1, grandfathername1): 
        self.fathername1 = fathername1 
 
        # here, we will invoke the constructor of Grandfather class 
        Grandfather1.__init__(self, grandfathername1) 
 
# here, we will create the Derived class 
class Son1(Father1): 
    def __init__(self,sonname1, fathername1, grandfathername1): 
        self.sonname1 = sonname1   
        Father1.__init__(self, fathername1, grandfathername1) 
 
    def print_name(self): 
        print('Grandfather name is :', self.grandfathername1) 
        print("Father name is :", self.fathername1) 
        print("Son name is :", self.sonname1) 
 
# Driver code 
s1 = Son1('John', 'John Jr', 'John Jr Jr'
print (s1.grandfathername1) 
s1.print_name()  

Output:

John Jr Jr
Grandfather name is : John Jr Jr
Father name is : John Jr
Son name is :John

Hierarchical Inheritance

If multiple derived classes are created from the same base, this kind of Inheritance is known as hierarchical inheritance. In this instance, we have two base classes as a parent (base) class as well as two children (derived) classes.

Types of inheritance Python

Example:

# Python program for demonstrating Hierarchical inheritance 
 
# Here, we will create the Base class  
class Parent1
    def func_1(self): 
        print ("This function is defined inside the parent class."
 
# Derived class1 
class Child_1(Parent1): 
    def func_2(self): 
        print ("This function is defined inside the child 1."
 
# Derivied class2 
class Child_2(Parent1): 
    def func_3(self): 
        print ("This function is defined inside the child 2."
 
# Driver's code 
object1 = Child_1() 
object2 = Child_2() 
object1.func_1() 
object1.func_2() 
object2.func_1() 
object2.func_3()  

Output:

The function is defined inside the Parent class.
The function is defined inside the child 1.
The function is defined inside the parent class.
The function is defined inside the child 2.

Hybrid inheritance

Hybrid Inheritance is a blend of more than one type of inheritance. The class is derived from the two classes as in the multiple inheritance. However, one of the parent classes is not the base class. It is a derived class. 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.