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.
ExampleLet 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.
In the previous example of birds and his varieties in which the class of birds is parent class and varieties are class child .
Exampleclass birds: def fly(self): print("It can fly") class crow(birds): def colour(self): print("Crow are black") obj=crow() obj.fly()
self is a parameter of current instance of class used to access to variable of the class.
In python,Inheritance are divided into six type.
When one parent class inherits only one child class then,it is known as single level inheritance.
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
When parent class inherits child class and grandparents class inherits parent class then,it is known as multi-level Inheritance.
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
Two or more parent class inherits a single child class then,it is known as Multiple Inheritance.
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
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.
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 is a combination of Single and Multiple inheritance,It is also called multipath algorithm.
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.