Inheritance in Python

Python supports inheritance as it is an object-oriented programming language. Inheritance is the phenomenon by which one class can inherit the characteristics and behaviour of another class, just the way an offspring receives the qualities of its parent.

In technical language, the parent class is termed as the Super-class & the child class is called as Sub-class or derived class or inherited class. The sub-class is derived from the super-class, and it gets access to the attributes and functions of the super-class.

Now, as you know that the derived class already owns the attributes of its parent class, you do not need to define them again. This provides the reusability of the code. Although, you can modify the attributes of the parent class and create new variables or functions in the child class.

Types of Inheritance in Python:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance

1. Single Inheritance: As the name says, Single inheritance involves a single parent class and single child class.

Syntax:
class Child_class(Parent_class)

2. Multilevel Inheritance: In multilevel inheritance, a class is derived from a child class creating different levels. This hierarchy is just like, grandfather > father > son. Here, the son inherits the properties of father and grandfather as well. However, there is no limit for the number levels of inheritance.

3. Multiple Inheritance: In multiple inheritance, there are multiple parents to a single child class. Thus, the child class inherits the properties of all the parent classes.

Syntax:
class Child_class(Parent_class1, Parent_class2 ... )

Example:

class Student(): #parent class.
    def __init__(self, roll):
        self.roll_no = roll 
    def show_roll(self):
        print('Roll No: ', self.roll_no)

class Marks(Student): #child class.
    def __init__(self, roll, n, m):
        Student.__init__(self, roll) #accessing __init__() method of parent class.
        self.name = n
        self.marks = m 
    def show_details(self):
        print('Name of the student: ', self.name)
        print('Marks: ', self.marks)

m = Marks(104, 'Rohit Raj', 90) #creating object of child class. 
m.show_roll() #accessing method of parent class using object of child class.
m.show_details()

Output:

Roll No: 104
Name of the student: Rohit Raj
Marks: 90

Explanation:

  • In the above program, first of all, a class named Student is created which holds:
    1) a data member as roll_no under __init__() method
    2) a method named show_roll() which prints the roll_no on the output screen.
  • In the next step, another class named Marks is created which inherits the class Student where:
    1) data member of the parent class ie roll_no is accessed.
    2) new data members including name & marks are created.
    3) a method show_details() is defined to display the name and marks.
  • Now, an object of the class Marks ie m is created and the required parameters (roll_no, name, marks) are passed to it.
  • This object is then used to invoke the method of the parent class i.e show_roll() and method of child class ie show_details().

issubclass() & isinstance() Method in Python

Python provides built-in methods to find out whether a class is inherited or not. Both of these functions return a boolean value i.e either 'True' or 'False'.

issubclass(): Two arguments are passed to this method, Child_class and Parent_class(in the same order.).

Now, if the specified child class is derived from the given parent class, the method returns True, otherwise False.

Let us see an example with reference to the previous code.

Example:

print(issubclass(Marks, Student))
print(issubclass(Student, Marks))

Output:

True
False

The output 'True' shows that Marks is a sub-class of Student whereas 'False' shows that Student is not a sub-class of Marks.

  • In Python, all the classes are sub-class of a predefined class called as object.

Example:

print(issubclass(Student, object))

Output:

True

isinstance(): This method also takes two parameters, object and class, sequentially.

The return value of the method is True if the provided object is an instance of the specified class, else it returns False.

Example:

print(isinstance(m,Marks))

Output:

True