Top Ad unit 728 × 90

Welcome !!! To The World of Programming

OOPS FEATURES

Oops

Python - Method Overriding

Implementation in Python

Python is a high-level, interpreted, versatile and Cross-Platform Compatibility programming language. It is known for its simplicity and readability. It was developed by Guido Van Rossum in 1991.

Python has gained widespread popularity in both the programming community and various industries due to its ease of use, extensive standard library, and broad range of applications including Web Development, Data Analysis, Scientific Computing, and Artificial Intelligence.

Method Overriding in Python


Method Overriding in Python
Method Overriding in Python

Method overriding in Python is a OOP concept where a subclass provides a specific implementation or a new for inherited method from its superclass. It allows to customize the behavior of the method in the subclass, while keeping the same method signature (i.e., method name and parameters and return-type)  as the method in the superclass. Method overriding is a key feature of object-oriented programming and is used to achieve run time polymorphism.

The class diagram shown above is implemented in Python. The Person class is defined as follows:

# Defining a parent class  
class Person(): 
     # protected data members
     _firstName = None 
     _lastName = None

     # Defining a Constructor
     def __init__(self,firstName,lastName):  
        self.firstName = firstName
        self.lastName = lastName  
    # Defining a parents method  
     def completeName(self):  
        print(self.firstName + " " + self.lastName) 
        
The Employee class is defined by inheriting from the Person class. The constructor is also defined to pass parameters required by Person class constructor. Python uses  Person.__init__(self, firstName, lastName) to pass parameters from Employee class(Sub class) to the Person class(Super class) from the constructor of the sub class.
The completeName() method gets inherited from the Person class. It displays the name of the Person in the First and Last name format. 
Method Overriding is required when name of the Employee is required in the Last and First name format. It is implemented by redefining  completeName() method in the Employee class with the same signature as demonstrated in the code below:

# Defining a child class  
class Employee(Person):      
    # Constructor  
    def __init__(self,firstName,lastName):  
        Person.__init__(self, firstName, lastName)
        #calls parent class constructor
        #passes required arguments to parent class Constructor
    # showing the child's method  
    def completeName(self):  
        print(self.lastName + " " + self.firstName)
        

Once the Person and the Employee classes are defined then can be tested using the following code.

#code to test the defined Person and Employee Class
# Test  code  
person = Person("Neeraj","Chopra")
person.completeName()
person = Employee("Neeraj","Chopra")   
person.completeName() 

The output of the above code is:

Neeraj Chopra
Chopra Neeraj

Wondering:

  • Why all that lengthy code?
  • Why not make change in Person class code to get the work done?
Remember in a typical development Scenario, a team of developers working who may be from different organizations. One developer has developed the Person class and shared as python library file with other developers. Source code is not shared therefore code is not available to update it. 
What if both the versions of the completeName() is required?

Method Overriding is feature of Object-Oriented Languages to achieve runtime polymorphism. 

However, the developer of Person may decide not to allows the users of his/her class to override than the developer can mark completeName() method with the two underscores (i.e. makes it private, hence not inheritable).

Python do not require any specific annotations or keywords like @Override (used in Java) to indicate method overriding.

 

Python - Method Overriding Reviewed by Syed Hafiz Choudhary on September 17, 2023 Rating: 5

1 comment:

Contact Form

Name

Email *

Message *

Powered by Blogger.