1. Home
  2. Docs
  3. Object Oriented Programming in Python
  4. Inheritance

Inheritance

 
What is Inheritance ?
 
Inheritance is one of 3 major pillars of Object Oriented Programming.
 
It promotes code re-usability,

which is sharing the implementation between classes.

Inheritance is like a family tree, we inherit features or traits from our parents.

In Object Oriented Programming, instead of traits,

child classes inherit parent class attributes and methods.

You might have heard term likes,

Base class, derived class, Parent class, Child class, Super class and Sub class.

An inherited class is called as, either Base class,

Parent class or Super class.

Likewise the an inheriting class is known as either,

Derived class or Child class or Sub class.

 
To understand inheritance,
 
Let’s consider two classes Person and Employee.

Person class has a first name, last name, and a method to get full name.
 
Person:
first_name,

last_name,

get_name()

 
Employee class has its own attributes, employee id, title

and method check employee status.
 
Employee:
employee_id

title

Now, We will now define these two classes as below.
 

Create Person class

 

class Person(object):

    def __init__(self,f_name, l_name):

        self.first_name = f_name

        self.last_name  = l_name

    def get_name(self):

        return self.first_name +" " + self.last_name

 

Create Employee class

 

class Employee(Person):

    def __init__(self,f_name, l_name,emp_id,emp_title ):

        Person.__init__(self,f_name,l_name)

        self.employee_id = emp_id

        self.title  = emp_title

    def isEmployee(self):

        return True


 

Create Person instance

 

p1 = Person("Elon","Mark")

print(p1.first_name)

print(p1.last_name)


print(p1.get_name())


output:

Elon
Mark
Elon Mark

 

Create an Employee instance

 

e1 = Employee("Alex","Branden",23456,'Director')



print(e1.first_name)

print(e1.last_name)


print(e1.get_name())


output:

Alex
Branden
Alex Branden

 
p1 is an instance of Person class,
 
with dot notation we have accessed first_name and last_name,

And we also used get_name() of Person class to get the full name of Person.

Which is straight forward.

Notice the second output.
 

How did we get ?

 

Alex

Branden

Alex Branden

 
When Employee class don’t have first_name, last_name attributes.
 

Its because, Employee class inherits Person class, and with this,

Employee inherits Person class first_name, last_name and get_name() attributes.

As e1 is an instance of Employee class,

it gets access to all its own and parent class attributes.

 

Initializing Parent in Child class

If a class doesn’t have __init__ methods, Python looks for this method

in its parent, if parent also doesn’t implement it, then checks it’s parent class,

As soon as it finds one, its stop further check.

Like classes we can also inherit __init__ methods in a child class,

And initialize parents attributes, known as Inheriting the Parent constructor.

 

Initializing, parent class attributes in child class

 

In child class __init__ method, your have seen below code.
 

Person.__init__(self,f_name,l_name)

 
Here Person class __init__ methods is invoked,

with required parameters.

There is another way of calling parent class __init__ method,
 

Using super() built-in function

 
by using super() built-in function, like below.
 

super().__init__(f_name, l_name)

 
Above we are calling __init__ method without class name,

super() resolves parent class name,

with Method Order Resolution (MRO) technique.

For now we will stick to the first style of initializing , which is quite intuitive.

Like in C++ Python supports multiple inheritance.

 

Multiple inheritance.

 
Python supports multiple inheritance.

Using multiple inheritance, a child can inherit,

attributes and methods from more than one class.

 
To understand multiple hesitance,
 
let’s create another PayRoll class.
 

class PayRoll(object):

    def __init__(self, salary,perks):

        self.salary = salary
        
        self.perks = perks
          
    def total_payout(self):
        
        return self.salary + self.perks
    
pr = PayRoll(12000, 300)

pr.total_payout()


output:

12300

 
We want Employee to have is salary, perk attributes

and calculate is total payout.

For this we have to inherit Payroll class,

Let’s redefine Employee class, so to inherit Person, and Payroll classes.

 

		
class Employee(Person, PayRoll):

    def __init__(self,f_name, l_name,e_sal, e_perks,emp_id,emp_title ):
        
        Person.__init__(self,f_name, l_name)
		
        PayRoll.__init__(self,e_sal, e_perks)
		
        self.employee_id = emp_id

        self.title  = emp_title

    def isEmployee(self):

        return True
		

 
While creating instance of Employee,

We need to pass parameters base parameters also.

 

Create an instance of EmployeeOrder of arguments

 
While creating an instance, parameters to be passed in the order classes inherited.

i.e first Person class parameters, then PayRoll parameters and

then Employee class parameters.

Parameters order
 
Person -> PayRoll -> Employee

Create instance of Employee

 

e1 = Employee("Robert","Woods",1200,400,12313,'Architect')

		
print(e1.first_name , e1.last_name)

print(e1.get_name())


print(e1.title , e1.employee_id)


print(e1.salary , e1.perks)

output:

Robert Woods

Robert Woods

Architect 12313

1200 400

		

 
Notice how we initialized Person and PayRoll attributes.

 

Person.__init__(self,f_name, l_name)
		
PayRoll.__init__(self,e_sal, e_perks)

 
Order of initializing the parent classes doesn’t matter,

but its good practice to initialize,in the order they inherited.