Inheritance In Python


What is Inheritance in Python?


Inheritance is one of the most important and powerful features of object-oriented programming. Inheritance refers to the property in which a class can inherit properties of another class that is the derived class can access all the properties, functions, and data members of the base class from which it has been inherited. To compare the feature with real-time, children get characteristics and behaviors inherited from their parents. Inheritance in Python is provided for the same purpose as explained.



Advantages of Inheritance:


  • Reusability of code – since one can inherit from the base class, the efficiency of the code increases and the size of the code also decreases instead of rewriting the code.
  • It forms a chain process i.e. if a child class is inherited from the base class, and if a new class is inherited from the child class, then all the properties of the base class will also be present in the new class.

Syntax:

Class base_class:
     Body of base_class
Class child_class (base_class):
     Body of child_class

Example

class Parent:
def __init__(self , fname, fage):
self.firstname = fname
self.age = fage
def view(self):
print(self.firstname , self.age)
class Child(Parent):
def __init__(self , fname , fage):
Parent.__init__(self, fname, fage)
self.lastname = "Machine Learning"
def view(self):
print(self.firstname ," came ", self.age , " years ago. Today, python is the base for" , self.lastname)
object = Child("Python" , '28')
object.view()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output

Python came 28 years ago. Today, python is the base for Machine Learning
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Types of inheritance:


  • Single inheritance
  • Multiple inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

Single inheritance


In single inheritance, only one child class can be inherited from the base class. Not more than one class can be used for inheritance.

Syntax:

Class base_class:
     Body of base_class
Class child_class (base_class):
     Body of child_class

Example

class Base:
def __init__(self , fname, fage):
self.firstname = fname
self.age = fage
def view(self):
print(self.firstname , self.age)
class Child(Base):
def __init__(self , fname , fage):
Base.__init__(self, fname, fage)
self.lastname = "Machine Learning"
def view(self):
print(self.firstname ," is ", self.age , " years old and he is a pro in " , self.lastname)
object = Child("Raj" , '21')
object.view()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output

Raj is 21 years old and he is a pro in Machine Learning
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Multiple inheritance


In multiple inheritance, a child class can be inherited from more than one base class. Unlike single inheritance, there is no restriction on the number of base classes for inheriting the properties.

Syntax:

Class Base1():
   Body of base1
Class of Base2():
   Body of base2
Class Child (Base1,Base2):
   Body of child

Example

class mom:
mom_name = ""
def mom(self):
print(self.mom_name)
class dad:
dad_name = ""
def dad(self):
print(self.dad_name)
class daughter(mom,dad):
def parents(self):
print("Father's name: ",self.dad_name)
print("Mother's name: ",self.mom_name)
object=daughter()
object.dad_name="raj"
object.mom_name="nandhini"
object.parents()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output

Father's name: raj
Mother's name: nandhini
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Multilevel inheritance


The multilevel inheritance is like a chain process. In this type, a class is derived from a base class and it becomes a base class for another class that is another new class is derived from this derived class.

Syntax:

Class Base:
    Body of base class
Class Derived1 (Base):
   Body of derived1 class
Class Derived2 (Derived1):
  Body of derived2 class

Example

class Granddad:
def __init__(self, grandfather_name):
self.grandfather_name = grandfather_name
class dad(Granddad):
def __init__(self, father_name, grandfather_name):
self.father_name = father_name
Granddad.__init__(self, grandfather_name)
class Son(dad):
def __init__(self,son_name, father_name, grandfather_name):
self.son_name = son_name
dad.__init__(self, father_name, grandfather_name)
def print_name(self):
print('Grandfather name :', self.grandfather_name)
print("Father name :", self.father_name)
print("Son name :", self.son_name)
s1 = Son('Lucky', 'Raj', 'Kamal')
s1.print_name()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output

Grandfather name : Kamal
Father name : Raj
Son name : Lucky
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Hierarchical inheritance


In hierarchical inheritance from a single base class, we can inherit multiple derived classes. There is no restriction on the number of derived classes.

Syntax:

Class Base:
   Body of base class
Class Derived1 (Base) :
   Body of derived1 class
Class Derived2 (Base) :
   Body of derived2 class

Example

class Details:
def __init__(self):
self.__id="<No Id>"
self.__name="<No Name>"
self.__gender="<No Gender>"
def setData(self,id,name,gender):
self.__id=id
self.__name=name
self.__gender=gender
def showData(self):
print("Id: ",self.__id)
print("Name: ", self.__name)
print("Gender: ", self.__gender)
class Employee(Details): #Inheritance
def __init__(self):
self.__company="<No Company>"
self.__dept="<No Dept>"
def setEmployee(self,id,name,gender,comp,dept):
self.setData(id,name,gender)
self.__company=comp
self.__dept=dept
def showEmployee(self):
self.showData()
print("Company: ", self.__company)
print("Department: ", self.__dept)
class Doctor(Details): #Inheritance
def __init__(self):
self.__hospital="<No Hospital>"
self.__dept="<No Dept>"
def setEmployee(self,id,name,gender,hos,dept):
self.setData(id,name,gender
self.__hospital=hos
self.__dept=dept
def showEmployee(self):
self.showData()
print("Hospital: ", self.__hospital)
print("Department: ", self.__dept)
def main():
print("Employee Object")
e=Employee()
e.setEmployee(1,"Prem Sharma","Male","gmr","excavation")
e.showEmployee()
print("\nDoctor Object")
d = Doctor()
d.setEmployee(1, "pankaj", "male", "aiims", "eyes")
d.showEmployee()
if __name__=="__main__":
main()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output

Employee Object
Id: 1
Name: Prem Sharma
Gender: Male
Company: gmr
Department: excavation
Doctor Object
Id: 1
Name: pankaj
Gender: male
Hospital: aiims
Department: eyes
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Hybrid inheritance


The hybrid inheritance is a combination of other types of inheritance. Mostly it is a combination of multilevel and multiple inheritance. This type of inheritance is not much used.

In general, multiple and multilevel inheritance is widely used in real-time problems. Single inheritance is used for simple applications.


Check this video for more:



Video credits– Edureka




Also Read: