Python – Inheritance

Let’s understand inheritance by taking an example of vehicle. When you think about vehicles there are so many types of vehicles like – cars, trucks, motorcycles etc. But they all are used for same purpose which is transportation. Talking about each vehicle individually, each vehicle either it is a car or truck, they have same purposes with individual characteristics.

For example – A car has 4 wheels and it has a roof whereas a motorcycle has 2 wheels and it has no roofs. A car is commute to work or vacation with family together. Whereas a motorcycle is used for road trip or racing.


The mechanism of deriving a new class from an old one (existing class) such that the new class inherit all the members (variables and methods) of old class is called inheritance.

Let’s see an example.

class Parent:
    def func1(self):
        print("This is a Parent Class Function.")

class Child(Parent):
    def func2(self):
        print("This is a Child Class Function.")


obj = Child()
obj.func1()
obj.func2()

#OUTPUT
This is a Parent Class Function.
This is a Child Class Function.

Super Class and Sub Class

The old class is referred to as the super class and the new class is called sub class.

  • Parent class is also known as base class or super class.
  • Child class is also known as derived class or sub class.

In the above example, Parent class is a base class or super class and Child class is sub class or derived class.

All classes in python are built from a single super class called ‘object’ so whenever you create a class in python, object will become super class for them internally.

class Phone(object):
#or
class Phone:

What are the advantage of Inheritance?

  • Python inheritance provides code reusability.
  • Reusability enhances readability.
  • As the code is reused, it leads to less development and hence less maintenance cost.
  • Inheritance reduces code redundancy.
  • Real world relationship

__Init__() Function

__init__() function is called automatically as soon as the object of a class is created. When you add an __init__() function to a parent class a child class will not be able to access the parent class methods. In order to overcome this the child class __init__() function overrides the parent class __init__() function.

Let’s understand with the below example.

class Parent:
    def __init__(self, firstname, age):
        self.firstname = firstname
        self.age  = age
    def view(self):
        print(self.firstname, self.age)


class Child(Parent):
    def __init__(self, firstname, age):
        Parent.__init__(self,firstname,age)
        self.lastname = 'Query'

    def view(self):
        print(self.age, self.lastname, self.firstname)

obj = Child(23, 'Share')
obj.view()

#OUTPUT
Share Query 23

In the above example, you can see how to override the parent class __init__() method using child class.

Type of Inheritance

Following are the types of inheritance in python.

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

Single Inheritance

When the inheritance involves single child class and single parent class only called single inheritance.

Example –

class Parent:
    def func1(self):
        print("This is Parent Class Function.")

class Child(Parent):
    def func2(self):
        print("This is Child Class Function.")

obj = Child()
obj.func1()
obj.func2()

#OUTPUT
This is Parent Class Function.
This is Child Class Function.

Multiple Inheritance

When the inheritance more than one parent class then it is multiple inheritance.

class Parent1:
    def func1(self):
        print("This is Parent1 Class Function.")
class Parent2:
    def func2(self):
        print("This is Parent2 Class Function.")
        
class Child(Parent1, Parent2):
    def func3(self):
        print("This is Child Class Function.")

obj = Child()
obj.func1()
obj.func2()
obj.func3()

#OUTPUT
This is Parent1 Class Function.
This is Parent2 Class Function.
This is Child Class Function.

Multilevel Inheritance

Inheritance in which the child class acts as a parent class for another child class is called multilevel inheritance.

class Parent1:
    def func1(self):
        print("This is Parent1 Class Function.")
class Parent2(Parent1):
    def func2(self):
        print("This is Parent2 Class Function.")

class Child(Parent2):
    def func3(self):
        print("This is Child Class Function.")

obj = Child()
obj.func1()
obj.func2()
obj.func3()

#OUTPUT
This is Parent1 Class Function.
This is Parent2 Class Function.
This is Child Class Function.

Hierarchical Inheritance

Inheritance which involves multiple inheritance from same parent class.

class Parent1:
    def func1(self):
        print("This is Parent1 Class Function.")

class Parent2(Parent1):
    def func2(self):
        print("This is Parent2 Class Function.")

class Child(Parent1):
    def func3(self):
        print("This is Child Class Function.")

obj = Child()
obj1 = Parent2()

obj.func1()
obj1.func1()

#OUTPUT
This is Parent1 Class Function.
This is Parent1 Class Function.

Hybrid Inheritance

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance.

class Parent1:
    def func1(self):
        print("This is Parent1 Class Function.")

class Parent2(Parent1):
    def func2(self):
        print("This is Parent2 Class Function.")

class Parent3:
    def func4(self):
        print('This is Parent3 Class Function.')

class Child(Parent1, Parent3):
    def func3(self):
        print("This is Child Class Function.")

obj = Child()
obj.func1()
obj.func4();

#OUTPUT
This is Parent1 Class Function.
This is Parent3 Class Function.
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial