Introduction to Method Overriding in Python
From the method overriding perspective, the class which has been declared initially is called the parent class. Any class which comes after this is termed as child class or the parent class. Here, overriding allows the child class or the subclass to use all the parent class’s attributes and functions in the most flexible manner possible. The only protocol on this is like the subclass entity should be holding similar parameters and arguments as like parent class. The method overriding is considered to be the most majorly mentioned overriding technique in python programming. This is one of the most effective representations in python.
object_1 = parent_class()
object_2 = Child_class()
Examples of Method Overriding in Python
Below are the examples of the method overriding:
self.Lowercase = Lowercase
self.Uppercase = Uppercase
print('Lower case variable type: ',type(Lowercase))
print('Upper case variable type: ',type(Uppercase))
print('Lower case alphabets: ',len(Lowercase), Lowercase)
print('Upper case alphabets: ',len(Uppercase), Uppercase)
self.PrimeNumbers = PrimeNumbers
def attribute_determining_method (self):
print('Collection used for variable3 :',type(PrimeNumbers))
print('Lower case alphabets : ', len(PrimeNumbers),PrimeNumbers)
Lowercase=['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'] Uppercase=['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'] PrimeNumbers=['1','3','5','7','11','13','17','19','29','31','37','41','43','47','53','59','61','67','71','73','79','83','89','97'] object_A = parent_class(Lowercase,Uppercase)
object_B = child_class(PrimeNumbers)
Explanation: The above program employs three lists where two in the middle of them asset the lower case as well as the upper case letters, the third one seizes the prime number ideals from 0 to 100. The flow of the program is considered in such an approach that the specifications and fillings of the corresponding lists are accepted to be printed. In this instance, two dissimilar classes are worn intended for this point. The parent class levers each and every compilation of alphabets while the child class levers the collection for prime numbers.
We can observe the function ‘attribute_determining_method()’ is affirmed as a piece of mutual classes. This method grips the characteristic dispensation for the alphabets; for the child class, it grips the characteristic dispensation for prime numbers. the noteworthy condition is the method name being the identical ones in both the affirmed classes.
So at the time an entity is instantiated intended for the parent class, it will be competent of triggering a function call to the function in a parent class, and for other perceptions, the object instantiated for child class would be proficient in initiating a function call to the method in child class. So this mentions when ‘object_B. attribute_determining_method ‘ is called it calls the function for the child class still in the attendance of the equivalent method in the parent class. So this obviously justifies the overriding of the child class method over the parent class affirmed by incorporated the detail that subclass is offered a meticulous kind of functioning in which the subclass element overrides the parent class constituents.
self.Enrolled_Name = input( " Enter Name of the Enrolled : " )
self.Enrolled_age = input( " Enter age of the Enrolled : " )
self.Enrolled_gender = input( " Enter gender of the Enrolled : " )
print( " \n \n Enter Name of the Enrolled : " , self.Enrolled_Name )
print( " Enter age of the Enrolled : " , self.Enrolled_age )
print( " Enter gender of the Enrolled : " , self.Enrolled_gender )
# Define a class as 'Estimated_Marks'
self.stuClass = input( " Class of the Enrolled : " )
print( " Estimated Marks per subject : " )
self.Science = int(input( " Mark in Science: " ))
self.maths = int(input( " Mark in Math: " ))
self.history = int(input( " Mark in history: " ))
self.hindhi = int(input( " Mark in Hindi: " ))
print( " Study in : " ,self.stuClass)
print( " Total Estimated_Marks : " , self. Science + self.maths + self.history+ self.hindhi)
class Enrolled(Individual, Estimated_Marks):
# Call ' Individual ' super class constructor
# Call ' Estimated_Marks ' super class constructor
# Call method of class 'Individual'
# Call method of class 'Estimated_Marks'
# Objects of class 'Enrolled'
Enrolled_1 = Enrolled()
Enrolled_2 = Enrolled()
print( " Note : The instances get initialized with the given value
successfully " )
Explanation: This program is another representation of the method overriding flow; it mentions how the method ‘display()’ is getting overridden by the parent and the child class mentioned. The program’s pseudo flow is to receive a given set of inputs from the user and wind up the flow. in the process of recovering the inputs from the user, it faces two different objects. both the objects are instantiated for the class ‘Enrolled’. Over that, we can notice that the result() method of the enrolled class gets instantiated just after the init constructor method. the result() mentioned inherits the method display() from its parent class. The display method in the parent class Estimated marks are used to calculate the sum of all the marks keyed for the different subjects associated with each of the students. This display method is overriden() by the parent methods it nicely displays the sum on to the corresponding console output. So the overriding process is achieved at the point where the display method already being a part of the super parent class individual, yet it was capable to get it overridden in the class estimated marks. this precisely depicts the capability of this method getting overridden in different instances.
The method overriding exhibits the implementation of the same class in more than one way. The regulation of overriding has no impact on the modifier used. More specifically, it describes the characteristics of the classes which are being involved. These cases make a structure the concept of superseding a very noteworthy structure in the python world.
This is a guide to Method Overriding in Python. Here we discuss the introduction, syntax, and examples of Method Overriding in Python with codes & outputs. You can also go through our other suggested articles to learn more –