Skip to content

Commit 2392199

Browse files
committed
Polymorphism
1 parent 7cb55c0 commit 2392199

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

07.Polymorphism/polymorphism-1.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#polymorphism-1.py
2+
3+
# Polymorphism means having the same interface/attributes in different
4+
# classes.
5+
6+
# Polymorphism is the characteristic of being able to assign
7+
# a different meaning or usage in different contexts.
8+
# A not-so-clear/clean example is, different classes can have
9+
# the same function name.
10+
11+
# Here, the class Dog and Cat has the same method named 'show_affection'
12+
# Even if they are same, both does different actions in the instance.
13+
#
14+
# Since the order of the lookup is
15+
# 'instance' -> 'class' -> 'parent class', even if the
16+
# 'class' and 'parent class' has functions with the same name,
17+
# the instance will only pick up the first hit,
18+
# ie.. from the 'class' and won't go to the parent class.
19+
20+
21+
class Animal(object):
22+
def __init__(self, name):
23+
self.name = name
24+
25+
def eat(self, food):
26+
print("{0} eats {1}".format(self.name, food))
27+
28+
29+
class Dog(Animal):
30+
def show_affection(self):
31+
print("{0} wags tail".format(self.name))
32+
33+
34+
class Cat(Animal):
35+
def show_affection(self):
36+
print("{0} purrs".format(self.name))
37+
38+
39+
for a in (Dog("Tuffy"), Cat("Mona"), Cat("Lucy"), Dog("Tommy")):
40+
a.show_affection()
41+
42+
'''
43+
O/P-
44+
Tuffy wags tail
45+
Mona purrs
46+
Lucy purrs
47+
Tommy wags tail
48+
'''

07.Polymorphism/polymorphism-2.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#polymorphism-2.py
2+
3+
# Another example for Polymorphism are the several inbuilt
4+
# functions in Python. Take for example, the builtin function
5+
# called 'len'.
6+
7+
# 'len' is available for almost all types, such as strings,
8+
# ints, floats, dictionaries, lists, tuples etc..
9+
# When len is called on a type, it actually calls the inbuilts
10+
# private function 'len' on that type or __len__
11+
12+
# Every object type that supports 'len' will have a private
13+
# 'len' function inbuilt.
14+
15+
# Hence, for example, a list type already has a 'len()'
16+
# function inbuilt in the Python code, and when you run the
17+
# len() function on the data type, it checks if the len
18+
# private function is available for that type or not.
19+
# If it is available, it runs that.
20+
21+
text = ["Hello", "Hola", "helo"]
22+
print("text",len(text))
23+
24+
print("hello",len("Hello"))
25+
print("char",len({"a": 1, "b": 2, "c": 3}))
26+
27+
'''
28+
O/P-
29+
text 3
30+
hello 5
31+
char 3
32+
'''

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,13 @@ Encapsulation involves the bundling of data members and functions inside a singl
139139

140140
[Detailed Explanation](06.Encapsulation)
141141

142+
------------
143+
------------
144+
#### 07. Polymorphism
145+
146+
The literal meaning of polymorphism is the condition of occurrence in different forms.
147+
Polymorphism in python defines methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. Also, it is possible to modify a method in a child class that it has inherited from the parent class.
148+
149+
[Detailed Explanation](07.Polymorphism)
150+
142151
------------

0 commit comments

Comments
 (0)