In single inheritance in python or in any language for that matter, a class is derived from only one parent class. The sub class thus, formed also derives its properties and behaviours’ from that one base class.

 Inheritance, is that case, wherein, an object  is based upon another class or an object, with characteristically similar implementations.

In inheritance, single inheritance is a much safer practice than multiple inheritance. If it is executed in a correct way, it enables the derived class to invoke parent class’s method, also, override the pre-existing parent class’s methods.

Single Inheritance and Multiple Inheritance Explained

This, thus helps in code reusability of parent class. It also helps in adding new characteristics to a class. Thereby, making the code simpler hence, easily readable, less redundant and more elegant.

Syntax:

# Parent_Class block
class Parent_Class:

#Child_Class block
class Child_Class(Parent_Class):

The Parent_Class above is the base class or the parent class, whereas, Child_Class is the derived class or the child class.

Working of Single Inheritance in Python — How?

Every class has its own block of code. Also, from the concept of single inheritance, each element present in the block of code of the base class can be implemented in the derived class. Also, to implement single inheritance syntactically, the parent class is mentioned within the parameters of the derived class.

# Example — 1

##### Single inheritance in python #####
# Parent/Base class
class Parent_Class(object):
# Creating Constructor
def __init__(self, credentials, no.):
self.name = credentials
self.id = no.
# To give employee details
def Employee_Deets(self):
return self.id , self.name
# To check for a valid employee
def Employee_Check(self):
if self.id > 40000:
return " Valid Employee "
else:
return " Invalid Employee "
# derived class
class Child_Class(Parent_Class):
def Finish(self):
print("END OF PROGRAM")
# Execution Code
Employee = Parent_Class( "Employee1" , 600545)  # It is base class object
print(Employee1.Employee_Deets() , Employee1.Employee_Check())
Employee2 = Child_Class( "Employee2" , 196744) # It is child class object
print( Employee2.Employee_Deets() , Employee2.Employee_Check() )
Employee2.Finish()

Output:

Single Inheritance in Python 1-2
Output for the above program

Detailed Explanation : Like all single inheritance pattern, here also, two different classes have been raised here. The base class or the parent class, here, holds three major segments.

  • Constructor Segment: Constructor is used to assign the parameter, the value of the declared object in the similar class.
  • Employee Deets Method : The employee deets segment fetches the employee name in the output screen.
  • Valid Employee Check Method : Here, the single inheritance concept is applied. This method is used to operationally check and verify whether an employee is a valid employee or an invalid employee. A conditional check condition such as if, the generated employee no. is more than 40000 then that employee no. is considered to be valid otherwise not.

Child Class is hence, declared beneath the parent class. The child class thus, carries an active display method which signifies the finish or the end of the program.

We also see that object for child class uses the items of the inherited base class to check and verify the validity and the details of the employee’s. Thus, quite correctly, the single inheritance concept is in the program.

#Example — 2

##### Hello World program #####
# Parent Class
class Parent_Class(object):
# Creating Constructor
def __init__(self, 1val,2val):
self.val1= 1val
self.val1= 2val
# Performing addition
def Add(self) :
print(" Addition value1 : " , self.val1)
print(" Addition value2 : " , self.val2)
return self.val1+ self.val2
def Product(self) :
print(" multiplication value1 : " , self.val1)
print(" multiplication value2 : " , self.val2)
return self.val1* self.val2
def Subtract(self) :
print(" subraction value1 : " , self.val1)
print(" subraction value2 : " , self.val2)
return self.val1 - self.val2
# Child Class
class Child_Class(Parent_Class):
pass
# Execution code
Object1 = Child_Class(10,15)  # It is base/parent class object
print("Added value :" , Object1.Add())
print(" ")
Object2 = Child_Class(20,30)  # It is parent class object
print("Multiplied value :" , Object2.Product())
print(" ")
Object3 = Child_Class(50,30)  # It is again the base class object
print("Subracted value :" , Object3.Subtract())

Output:

Single Inheritance in Python 1-3
Single Inheritance Example 2

Explanation: Declaration of two different classes takes place here again. Also, the base class or the parent class here, contains four major blocks of code.

  • Constructor Segment: Assigning of tasks takes place in this constructor segment.
  • Addition:  Addition of inputted values takes place.
  • Multiplication: Calculation of the product of inputted values takes place, hence.
  • Subtraction: Here, the result of subtraction of inputted values is obtained hence.

This program is also similar to the program in Example 1. The functional methods of the base class are also very wisely utilize in the derived class object. The child class though is not a physical section of the parent class. As a result of doing this, flow of the program is, as and how the requirement is.

The pass statement is therefore, required in the derived class to keep the execution of the program paused for a small duration.

Thus, for any method raised in the program, a new object’s declaration takes place. Also, to invoke the various methods, Instantiated objects used . The results are then, printed in the output screen. Thus, the single inheritance concept here again, is used and executed.

WRAPPING UP

OOPs or Object-Oriented Programming is an important reason behind the development of the high-level programming concepts, thus. OOPs acquired the characteristics of code flexibility and re-usability by significantly deeper depths.

Also, Inheritance is one of the most important and behaviorally significant concept in OOPs technique.

Also, Python programming language offers great flexibility in the programming area.