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

Python class and object tutorial with example

class and object are basic concept in object-oriented programming languages.a class is a collection of functions and variables, class is written by a programmer and should followed by a standard structure. object is simple an instance of a class.In this article we are going elaborate Python class and object tutorial with example.class in python is created by the keyword class.

So now we know the basic of a class,still wondering why we are using it and what are the advantages of class in python.

advantages of class in python

  • Re-usability
  • Data Redundancy
  • Security
  • Code Maintenance
  • Design Benefits. etc

Python class syntax

class Name_of_the_class:
    # Statement-1
    .
    .
    .
    # Statement-N

Python class and object tutorial with example code

class calculator:
    
   @staticmethod
   def sum(a, b):

     return a + b
   @staticmethod
   def multip(a, b):

     return a * b
   @staticmethod
   def div(a,b):

     return b / a
 
cal= calculator()
 
print(cal.sum(2,3))
print(cal.multip(2,3))
print(cal.div(2,3))  
Output 
5
6
1.5

Python class and object tutorial with example

Object of class in python

We know that object is an instance of a class, in simple Object of class in python is the copy of a class with actual values in it.In the above example cal is the object of the class calculator.we can create n number of object for a single class .example cal1= calculator() is the other instance of the class calculator() like this we can create n object for the class calculator().

__init__ method

__init__ method is a method in python ,it is like exactly like constructor concept in PHP or Java,__init__ is automatically called when an object of that class is created.__init__ method is normally used to initialize the data in a class.

Example of __init__ method in python

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

*args in python

*args in python is the special syntax used in python to pass n number of argument in a functions.so by using *args we can pass variable-length arguments to a function.

*args in python example

def sum(*argv):
   total=0;
   for num in argv:
       total += num
   return total

print(sum(1, 2, 3, 4, 5)) #output: 15




def sum(*argv):
   total=0;
   for num in argv:
       total += num
   return total

print(sum(1, 2, 3, 4, 5 ,6 ,7,8)) #output: 36

* args in python