4 Types of Inheritance in Python
Tags: python, 100daysofcode, beginners, programming
Published: 2022-10-05 02:49:13

_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) 
* [Multiple Inheritance](#Multiple Inheritance)
* [Multilevel Inheritance](#Multilevel Inheritance)
* [Hierarchical Inheritance](#Hierachical Inheritance)

***Single Inheritance*** ,<a name="Single Inheritance"></a>
Single inheritance- when a derived(child) class is derived from a single base(parent) class. 

![This is a diagram showing single inheritance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ir2pm6r9k04l6aviih1t.png)
Syntax
```python
# Base class
class Parent:
   def fun1(self):
      print("This is the parent class.")
class Child:
   def fun2(Parent):
      print("This is the child class.")
```
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***, <a name="Multiple Inheritance"></a>
Multiple inheritance- when derived(child) class is derived from **more** than one base(parent) class.

![This is a diagram showing multiple inheritance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1e55tla2g3dunl8dwa0.png)

```python
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

```
***Multilevel Inheritance***, <a>#Multilevel Inheritance</a>
Multilevel inheritance - The inheritance of a derived(child) class from another derived(child) class. 

![this is a diagram of multilevel inheritance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrxk4c49611kyna3vmir.png)
```python
class Base:
    pass

class Derived1(Base):
    pass

class Derived2(Derived1):
    pass

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

***Hierarchical Inheritance*** ,<a name="Hierarchical Inheritance"></a>
![This is a diagram of hierarchical inheritance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz3y5lii1y4ch49m5n0y.png)
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**.

```python
# 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.
```
Here we brought our diagram to life.