Introduction to Overriding in Python
In Overriding in Python, the object-oriented programming, the class which is declared initially is called the parent class. the one declared after this is called the subclass or the child class. In the Overriding in Python technique, the subclass is provided with a particular type of implementation in which the element in the subclass override the parent class element. According to this technique, the entity declared in the subclass must be similar in name, parameter, arguments with the parent class.so in such case, the subclass is said to override the parent class.
Function of Overriding in Python
The major type of overriding in python is method overriding. here a method declared in the parent class will be overridden in the subclass. Syntaxual representation of method overriding is represented below,
class parent_class: def overriding_method(self): class Child_class: def overriden_method(self): obj1 = parent_class() obj2 = Child_class() obj1.overriding_method() obj2.overriden_method()
class parent_class: def __init__(self,Lower_Case,Upper_Case): self.Lower_Case = Lower_Case self.Upper_Case = Upper_Case def attribute_finder_method(self): print('- - - - - - Overriding method output - - - - -') print(' - - - - PRINT ON ALPHABETS - - - - - ') print(' Collection used for variable1 : ', type(Lower_Case)) print(' Collection used for variable2 : ', type(Upper_Case)) print(' Lower case alphabets : ', len(Lower_Case), '--- Values -->',Lower_Case) print(' Upper case alphabets : ', len(Upper_Case), '--- Values -->',Upper_Case) print(' ') print(' ') print(' ') class child_class: def __init__(self,Prime_Numbers): self.Prime_Numbers = Prime_Numbers def attribute_finder_method(self): print('- - - - - - Overriden method output - - - - -') print(' - - - - PRINT ON PRIME NUMBERS - - - - - ') print(' Collection used for variable3 : ', type(Prime_Numbers)) print(' Lower case alphabets : ', len(Prime_Numbers), '--- Values -->',Prime_Numbers) Lower_Case = [ ' a ' , ' b ' , ' c ', ' d ' , ' e ' , ' f ', ' g ' , ' h ' , ' i ' , ' j ' , ' k ' , ' l ' , ' m ' , ' n ' , ' o ', ' p ' , ' q ' , ' r ', ' s ' , ' t ' , ' u ' , ' v ', ' w ' , ' x ' , ' y ' , ' z ' ] Upper_Case = [ ' A ' , ' B ' , ' C ', ' D ' , ' E ' , ' F ', ' G ' , ' H ' , ' I ' , ' J ' , ' K ' , ' L ' , ' M ' , ' N ' , ' O ', ' P ' , ' Q ' , ' R ', ' S ' , ' T ' , ' U ' , ' V ', ' W ' , ' X ' , ' Y ' , ' Z ' ] Prime_Numbers = [ ' 1 ' , ' 3 ' , ' 5 ', ' 7 ' , ' 11 ' , ' 13 ', ' 17 ' , ' 19 ' , ' 29 ' , ' 31 ' , ' 37 ' , ' 41 ' , ' 43 ' , ' 47 ' , ' 53 ', ' 59 ' , ' 61 ' , ' 67 ', ' 71 ' , ' 73 ' , ' 79 ' , ' 83 ', ' 89 ' , ' 97 '] object1 = parent_class(Lower_Case,Upper_Case) object1.attribute_finder_method() object2 = child_class(Prime_Numbers) object2.attribute_finder_method()
- The above program uses three lists, two among them holding the lower case and the upper case letters; the third one holds the prime number values from 0 to 100.
- The program’s functionality is designed so that the attributes and content of these lists are expected to be printed. In such a case, two different classes are used for this purpose. The parent class handles all the collection alphabets, whereas the child class handles the prime numbers collection.
- We can notice the function ‘attribute_finder_method()’ is declared as a part of both the classes.in the parent class, this method holds the attribute processing for alphabets, and in the child class, it holds the attribute processing for prime numbers. the significant specification is the function name being the same in both the declared classes.
- So when an object is instantiated for the parent class, then this object will be capable of initiating function call for the method in the parent class, and on another hand, the object instantiated for the child class will be capable of initiating function call for the method in child class. This means when ‘object2. attribute_finder_method()’ is called the method for the child class even in the same method in the parent class. This clearly justifies the overriding of the child class method over the parent class declared by assimilating the fact that the subclass is provided with a particular type of implementation in which the subclass element overrides the parent class element.
#!/usr/bin/evn python # Define a class as 'Individual' # class Individual: # Constructor#1 # def __init__(self): self.Student_Name = input( " Enter Name of the student : " ) self.Student_age = input( " Enter age of the student : " ) self.Student_gender = input( " Enter gender of the student : " ) # Method def display(self): print( " \n \n Enter Name of the student : " , self.Student_Name ) print( " Enter age of the student : " , self.Student_age ) print( " Enter gender of the student : " , self.Student_gender ) # Define a class as 'Evaluated_Marks' # class Evaluated_Marks: # Constructor#2 # def __init__(self): self.stuClass = input( " Class of the student : " ) print( " Evaluated Marks per subject : " ) self.literature = int(input( " Mark in Literature subject : " )) self.math = int(input( " Mark in Math subject : " )) self.biology = int(input( " Mark in Biology subject : " )) self.physics = int(input( " Mark in Physics subject : " )) # Method def display(self): print( " Study in : " ,self.stuClass) print( " Total Evaluated_Marks : " , self.literature + self.math + self.biology + self.physics) class student(Individual, Evaluated_Marks): def __init__(self): # Call ' Individual ' super class constructor Individual.__init__(self) # Call ' Evaluated_Marks ' super class constructor Evaluated_Marks.__init__(self) def result(self): # Call method of class 'Individual' Individual.display(self) # Call method of class 'Evaluated_Marks' Evaluated_Marks.display(self) # Objects of class 'student' # Student1 = student() Student2 = student() print(" ") print( " Note : The instances get initialized with the given values Successfully " )
Here the display() method is inherited and overridden, which achieves the concept of method overriding again.
Rules of Overriding in Python
- The method overridden must be the same name as the method specified in the parent class
- static methods cannot be overridden
- Final methods cannot be overridden
- The synchronized modifier has no consequence on the regulations of overriding.
The concept of overriding reflects multiple implementations of the same class. here the synchronized modifier has no consequence on the regulations of overriding. More priorly, it defines the behavior of the involved class very deeply. these cases make the concept of overriding a very significant one in the python world.
This is a guide to Overriding in Python. Here we discuss the basic concept, Function of Overriding and the Rules of Overriding in Python, respectively. You can also go through our other suggested articles to learn more –