Skip to content

Commit f37f3da

Browse files
committed
Method Overloading
1 parent b8ef046 commit f37f3da

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#method-overloading-1.py
2+
3+
# This code is an example on how we can extend a method inherited by
4+
# a child class from the Parent class.
5+
6+
# 1) We have defined `MyClass()` as an abstract class,
7+
# and it has three methods, my_set_val(), my_get_val(), and print_doc().
8+
# 2) MyChildClass() inherits from MyClass()
9+
# 3) MyChildClass() extends the parent's my_set_val() method
10+
# by it's own my_set_val() method. It checks for the input,
11+
# checks if it's an integer, and then calls the my_set_val()
12+
# method in the parent.
13+
14+
# 4) The print_doc() method in the Parent is an abstract method
15+
# and hence should be implemented in the child class MyChildClass()
16+
17+
18+
import abc
19+
20+
21+
class MyClass(object):
22+
23+
__metaclass__ = abc.ABCMeta
24+
25+
def my_set_val(self, value):
26+
self.value = value
27+
28+
def my_get_val(self):
29+
return self.value
30+
31+
@abc.abstractmethod
32+
def print_doc(self):
33+
return
34+
35+
36+
class MyChildClass(MyClass):
37+
def my_set_val(self, value):
38+
if not isinstance(value, int):
39+
value = 0
40+
super(MyChildClass, self).my_set_val(self)
41+
42+
def print_doc(self):
43+
print("Documentation for MyChild Class")
44+
45+
46+
my_instance = MyChildClass()
47+
my_instance.my_set_val(100)
48+
print(my_instance.my_get_val())
49+
print(my_instance.print_doc())
50+
51+
'''
52+
O/P-
53+
<__main__.MyChildClass object at 0x0000027750626FA0>
54+
Documentation for MyChild Class
55+
None
56+
'''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#method-overloading-1.py
2+
3+
import abc
4+
5+
6+
class GetSetParent(object):
7+
8+
__metaclass__ = abc.ABCMeta
9+
10+
def __init__(self, value):
11+
self.val = 0
12+
13+
def set_val(self, value):
14+
self.val = value
15+
16+
def get_val(self):
17+
return self.val
18+
19+
@abc.abstractmethod
20+
def showdoc(self):
21+
return
22+
23+
24+
class GetSetList(GetSetParent):
25+
def __init__(self, value=0):
26+
self.vallist = [value]
27+
28+
def get_val(self):
29+
return self.vallist[-1]
30+
31+
def get_vals(self):
32+
return self.vallist
33+
34+
def set_val(self, value):
35+
self.vallist.append(value)
36+
37+
def showdoc(self):
38+
print(
39+
"GetSetList object, len {0}, store history of values set".format(
40+
len(self.vallist)
41+
)
42+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#method-overloading-3.py
2+
3+
# We've seen that inherited methods can be overloaded.
4+
# This is possible using in-built functions as well.
5+
6+
# Let's see how we can overload methods from the `list` module.
7+
8+
9+
class MyList(list):
10+
def __getitem__(self, index):
11+
if index == 0:
12+
raise IndexError
13+
if index > 0:
14+
index -= 1
15+
return list.__getitem__(self, index)
16+
17+
def __setitem__(self, index, value):
18+
if index == 0:
19+
raise IndexError
20+
if index > 0:
21+
index -= 1
22+
list.__setitem__(self, index, value)
23+
24+
25+
x = MyList(["a", "b", "c"])
26+
print("First",x)
27+
print("-" * 10)
28+
29+
x.append("d")
30+
print("Second",x)
31+
print("-" * 10)
32+
33+
x.__setitem__(4, "e")
34+
print("Third",x)
35+
print("-" * 10)
36+
37+
print(x[1])
38+
print("Fourth",x.__getitem__(1))
39+
print("-" * 10)
40+
41+
print(x[4])
42+
print("Fifth",x.__getitem__(4))
43+
44+
'''
45+
O/P-
46+
First ['a', 'b', 'c']
47+
----------
48+
Second ['a', 'b', 'c', 'd']
49+
----------
50+
Third ['a', 'b', 'c', 'e']
51+
----------
52+
a
53+
Fourth a
54+
----------
55+
e
56+
Fifth e
57+
'''

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
06. [Encapsulation](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#06-encapsulation)
1010
07. [Polymorphism](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#07-polymorphism)
1111
08. [Decorators](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#08-decorators)
12+
09. [Method Overloading](https://github.com/Shikha-code36/Object-Oriented-Programming-OOPs-Python#08-decorators)
1213

1314
------------
1415
## What do you understand by OOPs?
@@ -161,4 +162,16 @@ This is also called metaprogramming because a part of the program tries to modif
161162

162163
[Detailed Explanation](08.Decorators)
163164

165+
------------
166+
------------
167+
#### 09. Method Overloading
168+
169+
Two methods cannot have the same name in Python. Method overloading in Python is a feature that allows the same operator to have different meanings.
170+
171+
Overloading is the ability of a function or an operator to behave in different ways based on the parameters that are passed to the function, or the operands that the operator acts on.
172+
173+
In Python, you can create a method that can be called in different ways. So, you can have a method that has zero, one or more number of parameters. Depending on the method definition, we can call it with zero, one or more arguments.
174+
175+
[Detailed Explanation](09.Method_overloading)
176+
164177
------------

0 commit comments

Comments
 (0)