> For the complete documentation index, see [llms.txt](https://gyansetu-python.gitbook.io/python-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-python.gitbook.io/python-programming/oops/overriding-methods-and-overloading-operators.md).

# Overriding Methods & Overloading Operators

**Overriding Method:**

Method overriding is a concept of object oriented programming that allows us to change the implementation of a function in the child class that is defined in the parent class. It is the ability of a child class to change the implementation of any method which is already provided by one of its parent class(ancestors).

Example 1:

```python
#!/usr/bin/python3
class Parent:        # define parent class
   def myMethod(self):
      print ('Calling parent method')
 
class Child(Parent): # define child class
   def myMethod(self):
      print ('Calling child method')
 
c = Child()          # instance of child
c.myMethod()         # child calls overridden method
```

Output:&#x20;

Calling child method

\
Example 2:

```python
class Rectangle():
	def __init__(self,length,breadth):
		self.length = length
		self.breadth = breadth
	def getArea(self):
		print(self.length*self.breadth," is area of rectangle")
class Square(Rectangle):
	def __init__(self,side):
		self.side = side
		Rectangle.__init__(self,side,side)
	def getArea(self):
		print (self.side*self.side," is area of square")
s = Square(4)
r = Rectangle(2,4)
s.getArea()
r.getArea()
```

16 is area of square&#x20;

8 is area of rectangle

**Overloading:**

* In Python you can define a method in such a way that there are multiple ways to call it.
* Given a single method or function, we can specify the number of parameters ourself.
* Depending on the function definition, it can be called with zero, one, two or more parameters.
* This is known as method overloading. Not all programming languages support method overloading, but Python does.

Example 3:

```python
#!/usr/bin/env python

class Human:

    def sayHello(self, name=None):
    
        if name is not None:
            print('Hello ' + name)
        else:
            print('Hello ')
        

# Create instance
obj = Human()
    
# Call the method
obj.sayHello()
    
# Call the method with a parameter
obj.sayHello('Guido')
```

Output:

Hello&#x20;

Hello Guido
