Inheritance in python example

Inheritance in python is the procedure in which one class inherits the properties from another class or in simple one class inherits the methods and attributes of another class.The class whose property being inherited is called parent class(base class) and the class which inherited property form parent class is called child class.Inheritance is one of the great example of code reusability in object-oriented programming language.

Inheritance in python with example

class Employee:
   
    # init method or constructor 
    def __init__(self, name,employee_id):
        self.employee_id = employee_id
        self.name = name
   
    # Sample Method 
    def welome_emp(self):
        
        print('Hello, welcome', self.employee_id, 'Your EMployee Id is', self.name)

class mechanical_depart(Employee):
      
    def depart_time(self):
        
         self.start_time = '10 am'
         self.close_time = '7 pm'
        
         print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time)
         
mech = mechanical_depart('Anamika','Emp1009')
mech.welome_emp()
mech.depart_time()

Inheritance in python example

In the above example , employee is the parent class and the mechanical_depart is the child class.so here when we create an object of mechanical department mech = mechanical_depart(‘Anamika’,’Emp1009′) .It actually using the parent class __init__ methos to assign the value of employee name and employee id.In the above mech is actually an object of mechanical_depart class but still we can use that object to call welome_emp, method inside his parent class Employee.
mech.welome_emp() is printing Hello, welcome Emp1009 Your Employee Id is Anamika out from the parent class Employee.

Like other OOPs language Python support different kind of Inheritance , Single Inheritance,Multi-level Inheritance, Multiple Inheritance and Hierarchical Inheritance

1) Hierarchical Inheritance in python

When more than one child class are inherited from the same parent class it is called Hierarchical Inheritance.In the blow image A is the parent class and B,C ,D and E are the child class derived from the parent class B this kind of inheritance is called Hierarchical Inheritance in python.

Hierarchical Inheritance in python

Example Hierarchical Inheritance in python

class Employee:
   
    # init method or constructor 
    def __init__(self, name,employee_id):
        self.employee_id = employee_id
        self.name = name
   
    # Sample Method 
    def welome_emp(self):
        
        print('Hello, welcome', self.name, 'Your EMployee Id is', self.employee_id)

class mechanical_depart(Employee):
      
    def depart_time(self):
        
         self.start_time = '10 am'
         self.close_time = '7 pm'
         print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time)

class IT_depart(Employee):
      
    def depart_time(self):
        
         self.start_time = '9.30 am'
         self.close_time = '7 pm'
         print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time)

class HR_depart(Employee):
      
    def depart_time(self):
        
         self.start_time = '8 am'
         self.close_time = '5 pm'
         print('Hello,', self.name ,' we will start at', self.start_time , 'and close at', self.close_time)
mech = mechanical_depart('Anamika','Emp1009')
mech.welome_emp()
mech.depart_time()

hr = HR_depart('Monica','Emp1009')
hr.welome_emp()
hr.depart_time()

Output 

Hello, welcome Anamika Your EMployee Id is Emp1009
Hello, Anamika  we will start at 10 am and close at 7 pm
Hello, welcome Monica Your EMployee Id is Emp1009
Hello, Monica  we will start at 8 am and close at 5 pm

2 ) Multiple Inheritance in python

When a child class is derived from more than one parent class that type of Inheritance is called Multiple Inheritance in python.In the below image A and B are parent class and C is the child class.So C inherit the property of both A and B.

Multipl Inheritance

Multiple Inheritance example in python



class Employee:
   
    # init method or constructor 
    def __init__(self, name,employee_id):
        self.employee_id = employee_id
        self.name = name
    # Sample Method 
    def welome_emp(self):
        
        print('Hello, welcome', self.name, 'Your EMployee Id is', self.employee_id)

class IT_depart():
      
    def depart_time(self):
        
         self.start_time = '10 am'
         self.close_time = '7 pm'
        
class system_depart(Employee,IT_depart):
      
    def depart_process(self):
        pass
        
system = system_depart('Joy','Emp1019')
system.welome_emp()
system.depart_time()
 Output 
Hello, welcome Joy Your EMployee Id is Emp1019

3) Multi-level Inheritance in python

As the name suggest Multi-level Inheritance in python is the way Y is inherited from X, then Z is inherited from Y, so Z is indirectly Z is derived from X and Y.

Multi level Inheritance

Multi-level Inheritance example in python

class Employee:
   
    # init method or constructor 
    def __init__(self, name,employee_id):
        self.employee_id = employee_id
        self.name = name
    # Sample Method 
    def welome_emp(self):
        
        print('Hello, welcome', self.name, 'Your EMployee Id is', self.employee_id)

class IT_depart(Employee):
      
    def depart_time(self):
        
         self.start_time = '10 am'
         self.close_time = '7 pm'
        
class system_depart(IT_depart):
      
    def depart_process(self):
        pass
        
system = system_depart('Joy','Emp1019')
system.welome_emp()
system.depart_time()

Output

Hello, welcome Joy Your EMployee Id is Emp1019

4) Single Inheritance in python

This is the simple Inheritance , in which child class derived from the one parent class

Single Inheritance in python

My Thought

Your email address will not be published. Required fields are marked *