Inheritance in Python

Here in this tutorial, we will learn how to create inheritance in Python. We will also discuss examples of method overriding and types of inheritance in Python.

A Program with Multiple Classes.

A program can have multiple classes, but we need to create an object of all the classes. Member of the class will access by their own object.

#creating class First
class First:
    def msg1(self):
        print("First Class")

#creating class Second
class Second:
    def msg2(self):
        print("Second Class")

#we need to create object of all the classes
f=First()
s=Second()

#member of the class will access by their own object
f.msg1()
s.msg2()

Python Inheritance Syntax

In inheritance, Parent(super) class is derived by the child(sub) class.

class ParentClass:
    #body of the base class

class ChildClass(ParentClass):
    #body of the derived class

Simple Inheritance in Python

Below example will show you, how to create inheritance in Python and how to access a member of the super class.

#creating class First
class First:
    def msg1(self):
        print("First Class")

#inheriting First class from Second class
        
class Second(First):
    def msg2(self):
        print("Second Class")

#we need to create object of sub class only
s=Second()

#we can access member of the both class using object of the sub class
s.msg1()
s.msg2()

Method Overriding in Python

When two-class have relationship of inheritance and both(parent and child) class have the same method name, it is known as method overriding.

class Transport:
    def speed(self):
        print("Each Transport has their own speed limit")

class Car(Transport):
    def speed(self):
        print("Car has max 150 mile/hr, speed limit")
mycar=Car()
mycar.speed()
mycar.speed()

In the above example, only the method of sub(child) class is called two times, because both classes share the same method name.

Using super() function

To call the method of a super(Parent) class in the child class, we will use the super() function.

class Transport:
    def speed(self):
        print("Each Transport has their own speed limit")

class Car(Transport):
    def speed(self):
        super().speed()     #calling method of super class
        print("Car has max 150 mile/hr, speed limit")
mycar=Car()
mycar.speed()

Constructor in Inheritance

As we know constructor(init()) of any class is called whenever an object is created. In inheritance, constructor without parameter of a super class is called automatically whenever we create an object of the subclass.

#Calling Default Constructor
class Product:
    def __init__(self):
        self.pid=1001
        
class Laptop(Product):
    pass

l1=Laptop()
print(l1.pid)

Calling Parameterised Constructor

To call the constructor with parameter of a super class, we will use superclass name.

class Product:
    def __init__(self,pid):
        self.pid=pid
        
class Laptop(Product):
    def __init__(self,pid):
        Product.__init__(self,pid)
    
l1=Laptop(1001)
print(l1.pid)

Types of Inheritance

In Python, there are three types of inheritance.

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance

Single Inheritance

In Single inheritance, one class can be derived from only one parent class.

class A:    #parent class
    def show(self):
        print("Class A")

class B(A): #child class
    def show(self):
        super().show()
        print("Class B")

b=B()
b.show()

Multiple Inheritance

In Multiple inheritance, one class can derive from more than one parent class.

class A:    #parent class
    def show(self):
        print("Class A")

class B:    #second parent class
    def show(self):
        print("Class B")

class C(A,B):   #child class, with two parent
    def show(self):
        A.show(self)
        B.show(self)
        print("Class C")
        
obj=C()
obj.show()

Multilevel Inheritance

In Multilevel inheritance, one child class can inherit from other child class.

class A:    #parent class
    def show(self):
        print("Class A")

class B(A):    #child of A
    def show(self):
        super().show()
        print("Class B")

class C(B):   #child of B
    def show(self):
        super().show()
        print("Class C")
        
obj=C()
obj.show()