Types of Inheritance in Python

  • December 12, 2022
  • python
JOIN Clause

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

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
Single Inheritance:

Single inheritance allows for the inheritance of properties from a single parent class by a derived class, allowing for the reuse of existing code and the addition of new features.

Example:

class Base:  
    def func_1(self):  
        print ("This function is defined inside the parent class.")  
  
# now, we will create the Derived class  
class Sub(Base):  
    def func_2(self):  
        print ("This function is defined inside the child class.")  
  
# Driver's code  
object = Sub()  
object.func_1()  
object.func_2()  

Output:

This function is defined inside the parent class.
This function is defined inside the child class.
Multiple Inheritance:

Because of this inheritance, a child class can derive from multiple parent classes. Classes in Java do not support this type of inheritance, whereas this type of inheritance is supported in Python. If we need to gather a variety of traits from various classes, it has a significant advantage.

Example:

class A:  
    name1 = ""  
    def A(self):  
        print(self.name1)  
  
# Here, we will create the Base class 2  
class B:  
    name2 = ""  
    def B(self):  
        print(self.name2)  
  
# now, we will create the Derived class  
class C(A, B):  
    def parents1(self):  
        print ("Father name is :", self.name2)  
        print ("Mother name is :", self.name1)  
  
# Driver's code  
s1 = C()  
s1.name2 = "AB"  
s1.name1 = "BC"  
s1.parents1()  

Output:

Father name is : AB
Mother name is : BC
Multilevel Inheritance :

In multilevel inheritance, characteristics from both the base class and the derived class are passed down to the new derived class. This is comparable to a relationship between a grandfather and a grandchild.

Example:

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

Output:

electric car runs on electricity
a car has 4 wheels
vehicles are used for transportation
Hierarchical Inheritance:

This type of inheritance is known as a hierarchical inheritance when multiple derived classes are produced from a single base class. Two child (derived) classes and a parent (base) class are present in this program.

Example:

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

Output:

#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()
Using issubclass() method:

The built-in function issubclass(paramOne, paramTwo), which can take either class names or class objects as arguments, allows us to determine whether a given class is a subclass of another class in Python.

Example:

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()
print(issubclass(Child, Parent))

Output:

This function is in parent class.
This function is in child class.
True

Note: also read about Inheritance in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

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