OVERRIDING AND OVERLOADING

Overriding in Python

 

Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

 

The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed.

Example:

class Parent():

      

    # Constructor

    def __init__(self):

        self.value = "Inside Parent"

          

    # Parent's show method

    def show(self):

        print(self.value)

          

# Defining child class

class Child(Parent):

      

    # Constructor

    def __init__(self):

        self.value = "Inside Child"

          

    # Child's show method

    def show(self):

        print(self.value)

          

          

# Driver's code

obj1 = Parent()

obj2 = Child()

  

obj1.show()

obj2.show()

 

Output:

Inside Parent

Inside Child

Method overriding with multiple and multilevel inheritance

Multiple Inheritance: When a class is derived from more than one base class it is called multiple Inheritance.

Example: Let’s consider an example where we want to override a method of one parent class only. Below is the implementation.

# Python program to demonstrate

# overriding in multiple inheritance

  

  

# Defining parent class 1

class Parent1():

          

    # Parent's show method

    def show(self):

        print("Inside Parent1")

          

# Defining Parent class 2

class Parent2():

          

    # Parent's show method

    def display(self):

        print("Inside Parent2")

          

          

# Defining child class

class Child(Parent1, Parent2):

          

    # Child's show method

    def show(self):

        print("Inside Child")

     

        

# Driver's code

obj = Child()

  

obj.show()

obj.display()

Output:

Inside Child

Inside Parent2

Using Super(): Python super() function

provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by ‘super’.

Example 1:

class Parent():

      

    def show(self):

        print("Inside Parent")

          

class Child(Parent):

      

    def show(self):

          

        # Calling the parent's class

        # method

        super().show()

        print("Inside Child")

          

# Driver's code

obj = Child()

obj.show()

Output:

Inside Parent

Inside Child

 


PYTHON OVERLOADING:

Like other languages (for example method overloading in C++) do, python does not supports method overloading by default. But there are different ways to achieve method overloading in Python.

 

The problem with method overloading in Python is that we may overload the methods but can only use the latest defined method.

# First product method.

# Takes two argument and print their

# product

def product(a, b):

    p = a * b

    print(p)

      

# Second product method

# Takes three argument and print their

# product

def product(a, b, c):

    p = a * b*c

    print(p)

  

# Uncommenting the below line shows an error    

product(4, 5)

product(4,5,6)

  

BY EXECUTING THIS WILL RAISE AN ERROR SINCE THE LATEST DEFINED FUCNTION IS TAKEN WITH THREE

ARGUMENTS.

What looks like overloading methods, it is actually that Python keeps only the latest definition of a method you declare to it. This code doesn’t make a call to the version of add() that takes in two arguments to add. So we find it safe to say Python doesn’t support method overloading. However, we recently ran into a rather Pythonic way to make this happen. Check this out:

1.        def add(instanceOf,*args):

2.        if instanceOf=='int':

3.        result=0

4.        if instanceOf=='str':

5.        result=''

6.        for i in args:

7.        result+=i

8.        return result

 

by using *args overloading can be solved

such as

In this code, not only do we use the *args magic variable for variable arity, we also let the code deal with both integers and strings. Watch it happen:

 

>>> add('int',3,4,5)

12

 >>> add('str','I ','speak ','Python')

‘I speak Python’

 

 

 

 



Comments