Introduction to Single Inheritance in Python
In python single inheritance, a derived class is derived only from a single parent class and allows the class to derive behaviour and properties from a single base class. This enables code reusability of a parent class, and adding new features to a class makes code more readable, elegant and less redundant. And thus, single inheritance is much more safer than multiple inheritances if it’s done in the right way and enables derived class to call parent class method and also to override parent classe’s existing methods.
Syntax:
class Parent_class_Name:
#Parent_class code block
class Child_class_Name(Parent_class_name):
#Child_class code block
The above syntax consists of two classes declared. One is the base class, or by other means, the parent class and the other one is for the child class, which acts as the derived class.
How Single Inheritance Works in Python?
Each of these classes has its own code block. So as per single inheritance, every element present in the parent class’s code block can be wisely used in the child class. To attain a single inheritance syntax ally, the name of the parent class is mentioned within the child class’s arguments.
Example #1
Code:
# Single inheritance in python
#Base class
class Parent_class(object):
# Constructor
def __init__(self, name, id):
self.name = name
self.id = id
# To fetch employee details
def Employee_Details(self):
return self.id , self.name
# To check if this is a valid employee
def Employee_check(self):
if self.id > 500000:
return " Valid Employee "
else:
return " Invalid Employee "
# derived class or the sub class
class Child_class(Parent_class):
def End(self):
print( " END OF PROGRAM " )
# Driver code
Employee1 = Parent_class( "Employee1" , 600445) # parent class object
print( Employee1.Employee_Details() , Employee1.Employee_check() )
Employee2 = Child_class( "Employee2" , 198754) # child class object
print( Employee2.Employee_Details() , Employee2.Employee_check() )
Employee2.End()
Output:
Explanation: Here, as like every single inheritance procedure, two different classes have been declared. The parent class is the base class, and the parent class holds three major sections.
- Constructor Section: The constructor has used for assignation the argument value of a declared object to the corresponding class.
- Method for Employee Details Fetches: The employee details section returns the employee name to the console output.
- Method for Employee Valid Check: This is the key area where the concept of single inheritance is applied. This method is operationally used to verify whether the given employee is valid or not. A sample check condition such that if the employee id generated is greater than five lakhs, then the id is considered valid and applied to that corresponding employee.
Below the parent class is declared the child class; the child class holds only an active display method that denotes the program’s end. So in the program driver area, two different objects are declared, one for the parent class and another for the child class. We can notice the child class’s object uses the elements of the inherited parent class to determine the employee details and employee validity. By this, the concept of single inheritance is nicely applied in the execution of the given an example.
Example #2
Code:
# Hello World program in Python
#Base class
class Parent_class(object):
# Constructor
def __init__(self, value1,value2):
self.value1 = value1
self.value2 = value2
# To perform addition
def Addition(self) :
print(" Addition value1 : " , self.value1)
print(" Addition value2 : " , self.value2)
return self.value1 + self.value2
def multiplication(self) :
print(" multiplication value1 : " , self.value1)
print(" multiplication value2 : " , self.value2)
return self.value1 * self.value2
def subraction(self) :
print(" subraction value1 : " , self.value1)
print(" subraction value2 : " , self.value2)
return self.value1 - self.value2
# derived class or the sub class
class Child_class(Parent_class):
pass
# Driver code
Object1 = Child_class(10,15) # parent class object
print(" Added value :" , Object1.Addition() )
print( " " )
Object2 = Child_class(20,30) # parent class object
print(" Multiplied value :" , Object2.multiplication() )
print( " " )
Object3 = Child_class(50,30) # parent class object
print("Subracted value :" , Object3.subraction() )
Output:
Explanation: Two different classes are declared again here. Here the base class, in other words, the parent class, the parent class holds four major code blocks.
- Constructor Section: All assignation is carried out in the constructor section.
- Method for Addition: Here addition of keyed input values is performed.
- Method for Multiplication: Here, the multiplication of keyed input values is performed.
- Method for Subtraction: Here, subtraction of keyed input values is performed.
Again this example is very similar to the above program where we use parent and child class inherited setup. Here, the parent class methods are nicely used by the child class object even though it is not a physical segment of that class itself. this clearly explains how the parent class content gets precisely inherited into the child class and keeps the program flow as needed. At the flow of execution, the keyed-in values and the outcome of methods are both printed in the console. the pass statement is used in the child class just to keep the execution paused for certain milliseconds of time.
In this program, a new object is declared for each and every method being declared. the instantiated object is then used to call the methods. the outcome from the methods is printed to the console. In that means the program prints three outcomes from the keyed in three input value pairs. they are like one for addition, one for multiplication and the last one is for subtraction.
Conclusion
Object-oriented programming is among the key reasons for the evolution of high-level programming. It pulled in code reusability and flexibility by greater means. Inheritance is among the significant concepts in object-oriented programming technique, and python offers an extensive amount of flexibility in the programming paradigm.
Recommended Articles
This is a guide to Single Inheritance in Python. Here we discuss how single inheritance works in python, along with examples and code implementation. You may also look at the following articles to learn more –