Different types of Inheritance in Python

Different types of Inheritance in Python

Table of Contents

  • What is inheritance.
  • Types of inheritance in Python
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

What is Inheritance?

One of the most important aspects of Object-Oriented Programming is Inheritance. Inheritance means, inheriting properties and methods from another class. Like, a child inherits some properties from a Father and some properties from a Mother. In the same way, we use inheritance in programming. Inheritance provides code reusability. We can use an existing class to create a new class instead of creating from scratch. Inheritance uses the DRY principle, which states DO NOT REPEAT YOURSELF.

In the inheritance, the child class inherits the properties and behaviors of the parent class. A child class can have their own properties and behaviors also.

Base class:

  • The class from which a class inherits.
  • The Base class is also called as Super Class, Parent class.

Derived Class:

  • The class that inherits from another class.
  • The derived class is also called a Subclass, Child class.

There are different types of inheritance. Types of inheritance depend upon the number of child and parent classes. Python supports four types of inheritance.

Types of inheritance in Python:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance

Let's take look at them one by one.

Single Inheritance:

Single Inheritance is inheritance where child class is inheriting from only one parent class.

Single.png

Example:

class Employee:
    def __init__(self, name):
        self.name = name

    def getName(self):
        return self.name


class Programmer(Employee):
    def getSalary(self):
        return 80000

web_developer = Programmer("Jane")
print(web_developer.getName())
print(web_developer.getSalary())

OUTPUT :

Jane
80000

In the above example, the base class is Employee, and the child class is Programmer. An employee can be a programmer. Hence, the Programmer class is inheriting the Employee class.

We are creating the object of the child class. From that child class, we are calling the method of the parent class getName().

Multiple Inheritance:

In the multiple inheritance child class inherits the properties and behaviors from more than one base class.

Multiple.png

class Addition:
    def addTwoInt(self):
        print("Addition is : ", self.num1 + self.num2)

class Subtraction:
    def subTwoInt(self):
        print("Subtraction is : ", self.num1 - self.num2)

class Multiplication:
    def mulTwoInt(self):
        print("Multiplication is : ", self.num1 * self.num2)

class Calculation(Addition, Subtraction, Multiplication):
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2


c = Calculation(10, 5);
c.addTwoInt();
c.subTwoInt();
c.mulTwoInt();

OUTPUT:

Addition is :  15
Subtraction is :  5
Multiplication is :  50

In the above example, We are creating three-class Addition, Subtraction, and Multiplication. From the Calculation class, we are inheriting those three classes.

Multilevel Inheritance:

In the multilevel Inheritance, one child class inherits the base class and that child class is inherited by another class. This is the same relationship as child, father, and grandfather.

Multilevel.png


class GrandFather:
    def __init__(self, grandFatherName):
        self.grandFatherName = grandFatherName

class Father(GrandFather):
    def __init__(self, fatherName, grandFatherName):
        super().__init__(grandFatherName)
        self.fatherName = fatherName

class Child(Father):
    def __init__(self, name ,fatherName, grandFatherName):
        super().__init__(fatherName, grandFatherName)
        self.name = name

    def getMyAncestorsName(self):
        print("My name is: ", self.name)
        print("My father name is: ", self.fatherName)
        print("My grand father name is: ", self.grandFatherName)

firstChild = Child('Sandhya', 'Ram', 'Gopal')
firstChild.getMyAncestorsName();


OUTPUT:

My name is:  Sandhya
My father name is:  Ram
My grand father name is:  Gopal

In the above example, we are creating a class GrandFather. The Father's class inherits the GrandFather's class. The Child class inherits the Father class. So, the Child class has properties of not only the Father class but also from GrandFather class. Father class is child class of GrandFather class and the same Father class is parent class of Child class.

the super() function is a special function, which give access to methods and properties of the parent class. the super() function returns the temporary object of the parent class.

Hierarchical Inheritance:

Hierarchical inheritance is where more than one child class is created from the single parent class.

Hierarchical.png


class Employee:
    def __init__(self, name):
        self.name = name

    def getName(self):
        return self.name

class Programmer(Employee):
    def getSalary(self):
        return 80000

class Tester(Employee):
    def getSalary(self):
        return 70000

WebDev = Programmer('Jane')
tester = Tester('Jack')

print("My name is", WebDev.getName(), "Salary is", WebDev.getSalary())
print("My name is", tester.getName(), "Salary is", tester.getSalary())

In the above example, we have created one parent class Employee. We have created two child classes Programmer and Tester which inherit the Employee class.

Conclusion:

In Object-Oriented Programming, inheritance is one of the most important topics. If you learn how to use it correctly you can easily create large-scale systems. Go ahead and try it on your own. Try to come up with more examples and code them. All the best.