Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 284acf7

Browse files
committed
code changes on polymorphism
1 parent f6413c6 commit 284acf7

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

OOP/Polymorphism/MethodOverloading.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def add(self, firstNum, secondNum):
1313
@desc: Takes two input paramter, adding them and returning
1414
@return: sum(firstNum, secondNum)
1515
'''
16-
return self.firstNum + self.secondNum
16+
return firstNum + secondNum
1717

1818

1919
# this method does the same as above, but the difference is, it takes 3 parameter here.
@@ -24,7 +24,15 @@ def add(self, firstNum, secondNum, thirdNum):
2424
@desc: takes three input parameter, adding them and returning
2525
@return : sum(firstNum, secondNum, threeNum)
2626
'''
27-
return self.firstNum + self.secondNum + self.thirdNum
27+
return firstNum + secondNum + thirdNum
28+
29+
objRam = SampleClass()
30+
# firstFunctionCall = objRam.add(10,20)
31+
secondFunctionCall = objRam.add(10,20,30)
32+
33+
objRam.special_add(1,2)
34+
35+
2836

2937
#Method overloading is not possible
3038
# This is compile time polymorphism.

OOP/Polymorphism/MethodOverloading_NotEfficient.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'''
55

66
# Function to take multiple arguments
7+
# *args is a list [1,2,3...]
78
def add(datatype, *args):
89

910
# if datatype is int
@@ -28,7 +29,24 @@ def add(datatype, *args):
2829

2930

3031
# Integer
31-
add('int', 5, 6)
32+
add('int', 5, 6,101,203,4354,54,65,76,4,6567,87,554,65,734,34,45,56,767,778)
3233

3334
# String
34-
add('str', 'Hi ', 'Geeks')
35+
add('str', 'Hi ', 'Geeks', "ram", "all", 'the', 'best')
36+
37+
38+
class sampleclass():
39+
40+
def __init__(self, *args):
41+
self.x = args
42+
43+
44+
def showMe(self):
45+
print(self.x)
46+
47+
48+
ramObj = sampleclass(10,20,30,'Sanjay')
49+
ramObj.showMe()
50+
51+
52+

OOP/Polymorphism/MethodOverriding.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
class Father(object):
2+
class Father():
33

44
def __init__(self, a, b):
55
self.a = a
@@ -18,7 +18,7 @@ class Mother(Father):
1818

1919
def __init__(self,):
2020
print ("b constructor")
21-
21+
2222
def role(self):
2323
print("Mother of Two childrens")
2424

@@ -28,6 +28,13 @@ def occupation(self):
2828
def whatCooking(self):
2929
print("Mother is preparing Pasta!")
3030

31+
32+
class Son(Father, Mother):
33+
pass
34+
35+
class Son(Mother):
36+
pass
37+
3138
# creating an object of mother.
3239
instanceOfMother = Mother()
3340

@@ -50,8 +57,4 @@ def whatCooking(self):
5057

5158
fatherInstance.occupation()
5259

53-
fatherInstance.whatCooking()
54-
55-
56-
# how about this?
57-
print(bb.a)
60+
fatherInstance.whatCooking() ## this will throw error

0 commit comments

Comments
 (0)