[go: up one dir, main page]

0% found this document useful (0 votes)
11 views17 pages

Polymorphism

Uploaded by

57xcqpxyf8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

Polymorphism

Uploaded by

57xcqpxyf8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Python.

Polymorphism
Lesson objectives
• 11.4.2.1 explain the concept of polymorphism with examples

Assessment criteria:
1. develop methods for the class
2. gives an example of polymorphism
What is Polymorphism?

Polymorphism is taken from the Greek words Poly (many) and


morphism (forms). It means that the same function name can be used
for different types. This makes programming more intuitive and easier.
Polymorphism gives you the ability to represent objects of different types
using a single interface.
A real-life example is You.
You act as a student when you are at college, you act like
a son/daughter when you’re at home, you act like a friend when you’re
surrounded by your friends.
Now the analogy here is, different personalities of that of a student,
a son/daughter, a friend are all analogous to objects of different types.
And a single interface (i.e., you) represents all these different types( i.e. your
different personalities).
Polymorphism - different behavior of the same method in different
classes.

Polymorphism is defined by a function that can process data of


different types.

The property of code to work with different data types is called


polymorphism.
Examples
One example is the function len().
We can define the length of string, list, tuple, etc.

print(len('school')) # 6
print(len([2, 3, 5, 7, 11, 13, 17, 19, 23])) # 9
print(len(('A', 'B', 'C'))) # 3
Other example, operation + (addition). In Python we can add whole
numbers, float numbers, strings, lists, etc.

print(5 + 3) #8
print(2.7 + 3.4) # 6.1
print('Hello, ' + 'world!') # Hello, world!
print([2, 3, 5] + [7, 11, 13]) # [2, 3, 5, 7, 11, 13]
print(len("Scaler Academy"))
print(len(["Python", "PHP", "C++"]))
print(len({"Name": "Rahul", "Address": "Kolkata,India"}))

Output:
14
3
2
Use addition function

def func_add(x, y):


return x + y

print(func_add(5, 3)) #8
print(func_add(2.7, 3.4)) # 6.1
print(func_add('Hello, ', 'world!')) # Hello, world!
print(func_add([2, 3, 5], [7, 11, 13])) # [2, 3, 5, 7, 11, 13]
Polymorphism with Function and Objects
class Tomato(): class Apple():
def type(self):
def type(self):
print("Fruit")
print("Vegetable") def color(self):
def color(self): print("Red")
print("Red") def func(obj):
obj.type()
obj.color()
obj_tomato = Tomato() Output:
obj_apple = Apple() Vegetable
func(obj_tomato) Red
Fruit
func(obj_apple) Red
class T1:
def __init__(self):
self.n = 10
def total(self, a):
return self.n + int(a)

class T2:
def __init__(self):
self.string = 'Hi'

def total(self, a):


return len(self.string + str(a))

t1 = T1()
t2 = T2()
print(t1.total(35)) # Вывод: 45
print(t2.total(35)) # Вывод: 4
Task1
Create multiple classes with different behavior of the same method
Task 2
Create classes vegetables and fruits with different behavior of the same
method
Task 3
• Create classes dog and cat with different behavior of the same
method

You might also like