Inheritance in Python
Inheritance is the main feature of the OOPs. It also exists in real life where a child class obtains all the visible property of their parents’ class.
Example
parent class
Child class
The above diagram clears that all property of class A is acquired by the class B
Type of Inheritance
- Single Inheritance: In this inheritance, parents class derived only single child class.
The diagram shows that class Country derived only a single child class India.
Syntax
class Country(): def hello(self): print("I am in Country Class") class India(Country): def hi(self): print("I am in India Class") obj=India() obj.hello()
Output
I am in India Class
- Multi-Level Inheritance: In multilevel inheritance derived class derives new child class.
In simple language, every derived class can be the parent class but in a singular manner.
Program:
class Grand_Father(): #parents class def gf(self): print("Grand Father") class Father(Grand_Father): #Intermediary class def f(self): print("Father") class Son(Father): def son(self): print("Son") obj=Son() obj.gf() obj.f() obj.son()
Output
Grand Father Father Son
- Multiple Inheritance: In multiple inheritance a derived Class can inherit the property of two base Class. In other language eg. Java does not support Multiple inheritance because this is an ambiguous situation for their correspondence compiler or interpreter. If class A and class B consist of the same name of methods named ‘hello’ then class C will be confused with Inherited class A method or class B method.
Python allows class C to inherit that particular class which inherits first in their arguments.
Lets understand by the program:
Program(First)
class A(): # parents class def ac(self): print("Class A") class B(): # parents class def ac(self): print("Class B") class C(A,B): # derived class None obj=C() obj.ac()
Output
Class A
Class C inherits class A and class B, and both classes consist of the same method ‘ac’ but class A first inherits class C then output shows class A .
Program (Second)
class A(): # parents class def ac(self): print("Class A") class B(): # parents class def ac(self): print("Class B") class C(B,A): # derived class None obj=C() obj.ac() obj.ac()
Output
Class B
In the same situation, first class B is inherited in class C, that’s why class B’s output is shown.
- Hierarchical Inheritance: In this inheritance, more than one child class is derived from a single parents class. This is called the Hierarchical Inheritance. Lets understand the diagram and program.
Program:
class Boss(): def bos(self): print("Boss") class Sales(Boss): def sal(self): print("Sales") class Technical(Boss): def tech(self): print("Technical") class worker_a(Sales): None class worker_b(Sales): None class worker_c(Technical): None class worker_d(Technical): None obj=worker_a() obj.sal() obj.bos() obj1=worker_b() obj1.sal() obj1.bos() obj2=worker_c() obj2.tech() obj2.bos() obj3=worker_d() obj3.tech() obj.bos()
Output
Sales Boss Sales Boss Technical Boss Technical Boss
- Hybrid Inheritance: This inheritance is a combination of all inheritance where there are multiple base classes and multiple drive classes.
Program:
class A: def fun(self): print("Class A") class B(A): def fun1(self): print("class B") class C(A): def fun2(self): print("Class C") class D(B,C): def fun3(self): print("Class D") obj=D() obj.fun() obj.fun1() obj.fun2() obj.fun3() obj.fun3()