Advertisement



< Prev
Next >



Python - Method Overriding





In Python, when a class is inherited, you can override the definition of any of its methods in its subclass and this procedure is known as method overriding. Hence in simple words, method overriding is overriding the definition of a superclass method in its subclass.

Note : Unlike the other languages like C++ and Java, which underline various rules to perform method overriding, Python only specifies a single rule which says that the method of superclass, which being overridden, must have the same name in the subclass as well.




Use of method overriding


The key benefit to method overriding is that you can alter the behaviour of the method inherited from a superclass and define its specification according to the need of the subclass. Let us understand this by an example.





A simple example of method overriding.


Let us create a class A which is inherited by another class B and the method of class A is overridden in class B.

# Python - Method overriding Example


# Superclass 
class A:

    # Method name() in class A 
    def name():		
        print('We are in class A')

        

# Subclass of class A
class B(A):
    
    # Method name() of class A is overridden in class B
    def name():		
        print('We are in class B')

In this example. we have class A inherited by class B. The method name() of class A is printing We are in class A but when this method is inherited and overridden by class B, it is printing We are in class B.


Advertisement




  • The overridden method does not have to return the same value.


  • Class Season is inherited by the class Summer and Autumn and the method message() of class Season is overridden by these subclasses, to give a specialized definition where each specialized version returns a different type of a value.

    # Base class
    class Season:			
        def message(self):
            print('Stunning Seasons')
            return 1
            
    
    
    
    
    # Derived class
    class Summer(Season):
        
        # method message() of base class Season is overridden 
        def message(self):
            print('Summer Sky')
            return 2.0
    
    
    # Derived class
    class Autumn(Season):
        
        # method message() of base class Season is overridden 
        def message(self):
            print('Autumn leaves')
            return 'Three'
    
    
    
    
    
    # Creating an object of Season
    ob_season = Season()
    
    # Using the object of Season class to call its method season()
    print(ob_season.message())
    
    
    
    # Creating an object of Summer
    ob_summer = Summer()
    
    # Using the object of Summer class to call its method season()
    print(ob_summer.message())
    
    
    
    # Creating an object of Autumn
    ob_autumn = Autumn()
    
    # Using the object of Autumn class to call its method season()
    print(ob_autumn.message())

    Output


    Stunning Seasons
    1
    Summer Sky
    2.0
    Autumn leaves
    Three

    We have called each specialized version of overridden method message() by using the object of its class and each specialized version has returned a different type of a value.




    Why method overriding is needed?


    Method overriding is mainly used in a situation when we want to create a more specialized version of a generic(general) method inherited from a superclass, within the subclass.



    Please share this article -





    < Prev
    Next >
    < Python super() function
    Python Polymorphism >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement