Inheritance in Python

Inheritance is a concept in Object-Oriented Programming (OOP) in Python, where one object acquires all the properties and behaviors of a parent object.
The motive behind inheritance is code reusability, enabling classes to have unique features that other classes can access if needed.

Example

Let we have a class of bird and and class of varieties of birds like(Parrot,owls,crow etc) in this class one things is common to all that is "they can fly",so using the concept Inheritance of we can write at one place and we can access in varieties class but birds class is not access any property of sub class.

Terms used in inheritance

Parent class and child class

In the previous example of birds and his varieties in which the class of birds is parent class and varieties are class child .

Example
class birds:
    def fly(self):
        print("It can fly")
class crow(birds):
    def colour(self):
        print("Crow are black")
obj=crow()
obj.fly()

self

self is a parameter of current instance of class used to access to variable of the class.

Types of Inheritance

In python,Inheritance are divided into six type.

Please REFRESH

Single Level Inheritance

When one parent class inherits only one child class then,it is known as single level inheritance.

Please REFRESH

Example
class parent:
    def printmainclass(self):
        print("This is Parent Class")
class child(parent):
    def printsubclass(self):
        print("This is child class")
obj=child()
obj.printmainclass()
Output
This is parent class

Multi-Level Inheritance

When parent class inherits child class and grandparents class inherits parent class then,it is known as multi-level Inheritance.

Please REFRESH
Example
class grand:
    def grandparent(self):
        print("This is Grand Parent Class")
class parent(grand):
    def printmainclass(self):
        print("This is Parent Class")
class child(parent):
    def printsubclass(self):
        print("This is child class")
obj=child()
obj.grandparent()
Output
This is grandparent class

Multiple Inheritance

Two or more parent class inherits a single child class then,it is known as Multiple Inheritance.

Please REFRESH
Example
class parent1:
    def parent1class(self):
        print("This is Parent1 Class")
class parent2:
    def parent2class(self):
        print("This is Parent2 Class")
class child(parent1,parent2):
    def chidclass(self):
        print("This is child class")
obj=child()
obj.parent1class()
obj.parent2class()
Output
This is Parent1 Class
This is Parent2 Class

Hierarchical Inheritance

In Hierarchical inheritance, more than one derived classes are created from a single base class and further child classes act as parent classes for more than one child classes,means,it forms a tree structure.

Please REFRESH
Example
class parent:
    def parentclass(self):
        print("This is Parent1 Class.")
class child1(parent):
    def child1class(self):
        print("This is child1 Class.")
class child11(child1):
    def child11class(self):
        print("This is child11 Class.")
class child12(child1):
    def child12class(self):
        print("This is child12 Class.")
class child2(parent):
    def chid2class(self):
        print("This is child2 class.")
class child21(child2):
    def child21class(self):
        print("This is child21 Class.")
class child22(child2):
    def child22class(self):
        print("This is child22 Class.")
obj1=child11()
obj2=child22()
obj1.parentclass()
obj2.parentclass()
Output
This is Parent1 Class.
This is Parent1 Class.

Hybrid Inheritance

Hybrid inheritance is a combination of Single and Multiple inheritance,It is also called multipath algorithm.

Please REFRESH
Example
class parent:
    def parentclass(self):
        print("This is Parent1 Class.")
class child(parent):
    def child1class(self):
        print("This is child1 Class.")
class child1(child):
    def child1class(self):
        print("This is child1 Class.")
class child2(child):
    def child2class(self):
        print("This is child2 Class.")
obj1=child1()
obj1.parentclass()
obj2.parentclass()
Output
This is Parent1 Class.
This is Parent1 Class.