4 Types of Inheritance in Python

kster

kster

Posted on October 5, 2022

4 Types of Inheritance in Python

Inheritance in medicine, describes the passing of genetic information from parent to child through genes. In programming it pretty much the same.
Inheritance in Python allows us to define a class that inherits all methods and properties from another class.

Base class (parent class) is the class being inherited from

Derived class (child class) is the class that inherits from another class

We will be going over 5 types of inheritances

Single Inheritance ,
Single inheritance- when a derived(child) class is derived from a single base(parent) class.

This is a diagram showing single inheritance
Syntax

# Base class
class Parent:
   def fun1(self):
      print("This is the parent class.")
class Child:
   def fun2(Parent):
      print("This is the child class.")
Enter fullscreen mode Exit fullscreen mode
  1. Creating the base class is the same creating any other class

  2. To create a class that inherits the functionalities from another class, you will need to input the parent class as a parameter when creating the child class.

Multiple Inheritance,
Multiple inheritance- when derived(child) class is derived from more than one base(parent) class.

This is a diagram showing multiple inheritance

class Prey:
    def flees(self):
        print("This animal is a hunter")

class Predator:
    def fighter(self):
        print("This animal is a hunter")

class Rabbit(Prey):
    pass

class Lion(Predator):
    pass

class Fish(Prey, Predator):
    pass

rabbit = Rabbit()
lion = Lion()
fish = Fish()

rabbit.flees() # output This animal is a hunter
lion.fighter() # output This animal is a hunter
fish.flees() # output This animal is a hunter
fish.fighter() # output This animal is a hunter

Enter fullscreen mode Exit fullscreen mode

Multilevel Inheritance, #Multilevel Inheritance
Multilevel inheritance - The inheritance of a derived(child) class from another derived(child) class.

this is a diagram of multilevel inheritance

class Base:
    pass

class Derived1(Base):
    pass

class Derived2(Derived1):
    pass

Enter fullscreen mode Exit fullscreen mode

Here we created a Base class, Derived1 class inherits from Base class and Derived2 class inherits Derived1.

Hierarchical Inheritance ,
This is a diagram of hierarchical inheritance
Hierarchical Inheritance is multiple derived classes are created from the same base class. In the image above we see that there is 1 parent class A that has multiple children B, C, D.

# Base class here
class Parent:
    def fun1(self):
        print("This function is in base class.")
#Derived class A
class ChildA(Parent):
    def fun2(self):
        print("This function is the derived class B")
#Derived class B here
class ChildB(Parent):
    def fun3(self):
        print("This function is the derived class C.")
#Derived class C here
class ChildC(Parent):
    def fun4(self):
        print("This function is the derived class D.")

object1 = ChildA()
object2 = ChildB()
object3 = ChildC()


object1.fun1()#Output This function is in base class.
object1.fun2()#Output This function is the derived class B
object2.fun1()#Output This function is in base class.
object2.fun3()#Output This function is the derived class C.
object3.fun1()#Output This function is in base class.
object3.fun4()#Output This function is the derived class D.
Enter fullscreen mode Exit fullscreen mode

Here we brought our diagram to life.

💖 💪 🙅 🚩
kster
kster

Posted on October 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related