Python Inheritance

  • Python

Inheritance is when you inherit properties/methods of another class onto the current class. In this article, we’ll learn about base class, child class, superclass, and different types of Inheritance in python.

The basic structure of Inheritance in python is –

class Parent:
  pass

class Child(Parent):
  pass

In the above example, class Parent is the base class and Child is the derived class or child class.

Now, Inheritance is of different types. Single level Inheritance, Multi level Inheritance and Multiple Inheritance.

Single level Inheritance

This type of inheritance in python involves two classes, and that is parent class and the child class.

class X:
  def func1(self):
    print("Function 1")

  def func2(self):
    print("Function 2")

class Y(X):
  def func3(self):
    print("Function 3")

obj1 = Y()
obj1.func1()
obj1.func2()
obj1.func3()

Output :

Function 1
Function 2
Function 3

In the above example, we inherited from class X which is the parent class for Y. obj1 which is an object for class Y was able to access the functions in class X. This is known as Inheritance or Single Level Inheritance.

Multi level Inheritance

This involves multiple classes inheriting each other. Let’s understand this using an example.

class X:
  def func1(self):
    print("Function 1")

  def func2(self):
    print("Function 2")

class Y(X):
  def func3(self):
    print("Function 3")

class Z(Y):
  def func4(self):
    print("Function 4")

obj = Z()
obj.func1()
obj.func3()
obj.func4()

Output :

Function 1
Function 3
Function 4

In the above example, class Z was able to access the method from class X as class Z is being derived from class Y and class Y is being derived from class X. This is known as Multi-Level Inheritance.

Multiple Inheritance

This is an example where a class inherits properties from different classes which are not inheriting from each other.

class X:
  def func1(self):
    print("Function 1")

  def func2(self):
    print("Function 2")

class Y():
  def func3(self):
    print("Function 3")

class Z(X,Y):
  def func4(self):
    print("Function 4")

obj = Z()
obj.func1()
obj.func3()
obj.func4()

Output :

Function 1
Function 3
Function 4

In this example, class Z is being derived from two different classes X and Y.

Method Resolution in Python (MRO)

MRO is another concept when we talk about Multiple Inheritance. This tells us that in python, attributes aor methods are first searched within the current class. If not found, it searches from left to right.

Let’s understand this using the below example.

class X:
  def func1(self):
    print("Function 1")

  def func2(self):
    print("Function 2-X")

class Y():
  def func2(self):
    print("Function 2-Y")

class Z(X,Y):
  def func1(self):
    print("Function 1-Z")

obj = Z()
obj.func1()
obj.func2()

Output :

Function 1-Z
Function 2-X

In the above example, we defined methods with same name func1 and func2 in classes X,Y and Z. Python using the MRO concept, first searched func1 within itself and returned the output. Then it started searching for func2 which he found in class X which proves the concept of moving from left to right.