Types of inheritance Python

Inheritance is defined as the capability of one class to derive or inherit the properties from some other class and use it whenever needed. Inheritance provides the following properties:

  • It represents real-world relationships well.
  • It provides reusability of code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
  • It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# A Python program to demonstrate
# inheritance  
  
  
# Base class or Parent class
class Child: 
  
    # Constructor
    def __init__(self, name): 
        self.name = name 
  
    # To get name
    def getName(self): 
        return self.name 
  
    # To check if this person is student 
    def isStudent(self): 
        return False
  
# Derived class or Child class
class Student(Child):
  
    # True is returned
    def isStudent(self): 
        return True
  
   
# Driver code 
# An Object of Child 
std = Child("Ram")
print(std.getName(), std.isStudent()) 
  
# An Object of Student 
std = Student("Shivam"
print(std.getName(), std.isStudent())

chevron_right


Output:

Ram False
Shivam True

Types of Inheritance in Python

Types of Inheritance depends upon the number of child and parent classes involved. There are four types of inheritance in Python:



  1. Single Inheritance: Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and addition of new features to existing code.

    single-inheritance

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to demonstrate
    # single inheritance
      
      
    # Base class
    class Parent:
         def func1(self):
              print("This function is in parent class.")
      
    # Derived class
    class Child(Parent):
         def func2(self):
              print("This function is in child class.")
      
    # Driver's code
    object = Child()
    object.func1()
    object.func2()

    chevron_right

    
    

    Output :-
    This function is in parent class.
    This function is in child class.
    

  2. Multiple Inheritance: When a class can be derived from more than one base classes this type of inheritance is called multiple inheritance. In multiple inheritance, all the features of the base classes are inherited into the derived class.

    multiple-inheritance1

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to demonstrate
    # multiple inheritance
      
      
    # Base class1
    class Mother:
        mothername = ""
        def mother(self):
            print(self.mothername)
      
    # Base class2
    class Father:
        fathername = ""
        def father(self):
            print(self.fathername)
      
    # Derived class
    class Son(Mother, Father):
        def parents(self):
            print("Father :", self.fathername)
            print("Mother :", self.mothername)
      
    # Driver's code
    s1 = Son()
    s1.fathername = "RAM"
    s1.mothername = "SITA"
    s1.parents()

    chevron_right

    
    

    Output:

    Father : RAM
    Mother : SITA
    

  3. Multilevel Inheritance
    In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and grandfather.



    Multilevel-inheritance1

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to demonstrate
    # multilevel inheritance
      
      
    # Base class
    class Grandfather:
        grandfathername ="" 
        def grandfather(self):
            print(self.grandfathername)
      
    # Intermediate class
    class Father(Grandfather):
        fathername = ""
        def father(self):
            print(self.fathername)
      
    # Derived class
    class Son(Father):
        def parent(self):
            print("GrandFather :", self.grandfathername)
            print("Father :", self.fathername)
      
    # Driver's code
    s1 = Son()
    s1.grandfathername = "Srinivas"
    s1.fathername = "Ankush"
    s1.parent()

    chevron_right

    
    

    Output:

    GrandFather : Srinivas
    Father : Ankush
    

  4. Hierarchical Inheritance: When more than one derived classes are created from a single base this type of inheritence is called hierarchical inheritance. In this program, we have a parent (base) class and two child (derived) classes.

    Hierarchical-inheritance1

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to demonstrate
    # Hierarchical inheritance
      
      
    # Base class
    class Parent:
          def func1(self):
              print("This function is in parent class.")
      
    # Derived class1
    class Child1(Parent):
          def func2(self):
              print("This function is in child 1.")
      
    # Derivied class2
    class Child2(Parent):
          def func3(self):
              print("This function is in child 2.")
       
    # Driver's code
    object1 = Child1()
    object2 = Child2()
    object1.func1()
    object1.func2()
    object2.func1()
    object2.func3()

    chevron_right

    
    

    Output:

    This function is in parent class.
    This function is in child 1.
    This function is in parent class.
    This function is in child 2.
    

  5. Hybrid Inheritance: Inheritence consisting of multiple types of inheritence is called hybrid inheritence.

    Hybrid-inheritance1

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python program to demonstrate
    # hybrid inheritance
      
      
    class School:
         def func1(self):
             print("This function is in school.")
       
    class Student1(School):
         def func2(self):
             print("This function is in student 1. ")
       
    class Student2(School):
         def func3(self):
             print("This function is in student 2.")
       
    class Student3(Student1, School):
         def func4(self):
             print("This function is in student 3.")
       
    # Driver's code
    object = Student3()
    object.func1()
    object.func2()

    chevron_right

    
    

    Output:

    This function is in school.
    This function is in student 1. 
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.