-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmethod-overloading-1.py
56 lines (39 loc) · 1.34 KB
/
method-overloading-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#method-overloading-1.py
# This code is an example on how we can extend a method inherited by
# a child class from the Parent class.
# 1) We have defined `MyClass()` as an abstract class,
# and it has three methods, my_set_val(), my_get_val(), and print_doc().
# 2) MyChildClass() inherits from MyClass()
# 3) MyChildClass() extends the parent's my_set_val() method
# by it's own my_set_val() method. It checks for the input,
# checks if it's an integer, and then calls the my_set_val()
# method in the parent.
# 4) The print_doc() method in the Parent is an abstract method
# and hence should be implemented in the child class MyChildClass()
import abc
class MyClass(object):
__metaclass__ = abc.ABCMeta
def my_set_val(self, value):
self.value = value
def my_get_val(self):
return self.value
@abc.abstractmethod
def print_doc(self):
return
class MyChildClass(MyClass):
def my_set_val(self, value):
if not isinstance(value, int):
value = 0
super(MyChildClass, self).my_set_val(self)
def print_doc(self):
print("Documentation for MyChild Class")
my_instance = MyChildClass()
my_instance.my_set_val(100)
print(my_instance.my_get_val())
print(my_instance.print_doc())
'''
O/P-
<__main__.MyChildClass object at 0x0000027750626FA0>
Documentation for MyChild Class
None
'''