Friday, May 3, 2019

Inheritance in Python

Inheritance is one of the four fundamental OOPS concepts. The other three being-

What is inheritance

Inheritance is a mechanism, by which one class acquires, all the properties and behaviors of another class. The class whose members are inherited is called the Super class (or base class), and the class that inherits those members is called the Sub class (or derived class).

Syntax of inheritance in Python

In Python a derived class can inherit a base class using the following syntax.

class DerivedClass(BaseClass):

Benefits of inheritance

Using inheritance you can write hierarchical code where you move from generic code (in Parent class) to specific code (in child class).

Inheritance OOPS concept relates to removing redundancy and write reusable code. Let’s try to clear it with an example.

Python inheritance example

Suppose we have an Account class with fields name, accountNum and methods to withdraw and deposit. There is another class PrivilegedAccount which has the same fields as in Account and provides the same methods to withdraw and deposit. Apart from that privileged account also provides extra facility to schedule appointment. Using inheritance PrivilegedAccount can inherit fields and methods of Account class (reuse that functionality) and just have extra method for appointment scheduling facility.

class Account:
  def __init__(self, name, acct_num): = name
    self.acct_num = acct_num

  def withdraw(self, amount):
    print('Withdrawing amount for ', self.acct_num)

  def deposit(self, amount):
    print('Depositing amount for ', self.acct_num)

class PrivilegedAccount(Account):
  def scheduleAppointment(self):
    print('Scheduling appointment for ', self.acct_num)

#PrivilegedAccount class object
pa = PrivilegedAccount('Ian', 1001)


Withdrawing amount for  1001
Depositing amount for  1001
Scheduling appointment for  1001

In the example, object of PrivilegedAccount is created since it has inherited the methods of the Super class so withdraw and deposit methods can be called on the PrivilegedAccount class object.

Constructor overriding and method overriding with inheritance

When a class inherits another class in Python, constructor in the super class is also available by default to the child class. You may have some extra fields in the child class which you need to initialize in the child class thus you can override the constructor in the child class to initialize the fields there.

To change the implementation of an already defined method in super class you can override it in child class this concept is known as method overriding.

Suppose there is a class Person with fields name, age and there is a method displayData to display those fields. There is another class Employee with fields name, age, empId and there is a method displayData to display those fields. Employee class is almost similar to Person class except an additional field empId so Employee class reuses Person class by inheriting it.

class Person:
  def __init__(self, name, age): = name
    self.age = age

  def displayData(self):
    print('In parent class displayData method')

class Employee(Person):
  def __init__(self, name, age, id):
    # calling constructor of super class
    super().__init__(name, age)
    self.empId = id

  def displayData(self):
    print('In child class displayData method')
    #calling super class method

#Employee class object
emp = Employee('John', 40, 'E005')


In child class displayData method
In parent class displayData method

As you can see only the empId field is initialized in the __init__() function of the Employee class, for initializing the other two fields __init__() function of the Person class (Base class) is called using the super() method.

Method overriding is also used in this example where the parent class’ displayData() method is overridden in the child class. For calling the displayData() method of the parent class again super() is used.

Types of Inheritance

As per OOPS concepts there are 5 types of Inheritance.

Single Inheritance

In this type of inheritance a sub class is derived from a single Super class. We have already seen Python examples of single inheritance above.

Single Inheritance in Java

Multi-level inheritance

In multi-level inheritance, a subclass inherits from another sub class which in turn may inherit from another subclass thus having levels (parent – child – grand child) of hierarchy.

class Parent:
#inherits Parent
class Child(Parent):
#inherits Child
class GrandChild(Child):
Multi-level inheritance in java

Multiple Inheritance

In multiple inheritance, a sub class is created using more than one super class.

class Parent1:
class Parent2:
#inherits Parent1 and Parent2
class Child(Parent1, Parent2):
Multiple Inheritance

Hierarchical Inheritance

In this type of inheritance more than one sub classes are created from the same super class.

class Parent:
#inherits Parent
class Child1(Parent):
#inherits Parent
class Child2(Parent):
Hierarchical Inheritance

Hybrid inheritance

Hybrid inheritance is the combination of more than one inheritance types. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multiple inheritance.

Hybrid inheritance

That's all for this topic Inheritance in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page

Related Topics

  1. Python Installation on Windows
  2. Operator Overloading in Python
  3. Abstract Class in Python
  4. Local, Nonlocal And Global Variables in Python
  5. Python return Statement With Examples

You may also like-

  1. self in Python
  2. Removing Spaces From String in Python
  3. Python Program to Display Fibonacci Series
  4. Global Keyword in Python With Examples
  5. Installing Ubuntu Along With Windows
  6. How to read file from the last line in Java
  7. Dependency Injection in Spring Framework
  8. Word Count MapReduce Program in Hadoop