Learn-Object_Oriented_programming-with-us
Learn By Topic:
Class-objects
Inheritance
Polymorphism
Encapsulation
Abstraction
Click here to toggle b/w normal and focus mode: FOCUS MODE
What is Polymorphism ?
Polymorphism is an important concept in OOP where we can use a single type of entity(method, operator or object) to refer to different types in different cases.
Simply put.. The word itself defines the meaning of a single entity being used as different operations at different scenarious..
Advantages of Polymorphism
The following are the advantages of polymorphism :
With this you can reuse the codes multiple times, i.e., classes once written, tested and implemented can be reused as required. Which means we can save a lot of time.
Multiple data types can be stored in single variable.
Easy to debug the code.
Polymorphism Examples
Operators as polymorphism :
The following is a simple example on how plymorphism works in python in genral.
n1=10
n2=9
print(n1+n2)
Output :
19
a='Mowli'
b='Sai'
print(b+' '+a)
Optput :
Sai Mowli
As you can see here , the same operator '+' can perform both addition on arithmetic numbers and concatination on strings.
This is a common example of polymorphism in OOP where a single entity can perform different data types in different cases.
Polymorphism Using Classes
If you are to store the data of students and teachers we can use polymorphism to do so as follows :
class Student:
def __init__(self, name, age,intrst):
self.name = name
self.age = age
self.intrst = intrst
def info(self):
print(f"I am a Student. My name is {self.name}. I am {self.age} years old.")
def intrest(self):
print(f"I want to learn {self.intrst}")
class Teacher:
def __init__(self, name, age,intrst):
self.name = name
self.age = age
self.intrst = intrst
def info(self):
print(f"I am a Professor. My name is {self.name}. I am {self.age} years old.")
def intrest(self):
print(f"I can teach {self.intrst}")
std1 = Student("Sasi", 19,"Polymorphism")
std2 = Student("Mowli",19,"Inheritance")
tch1 = Teacher("Venkatesh", 45,"Polymorphism")
tch2 = Teacher("Sharma",38,"Inheritance")
for S in (std1, tch1, std2,tch2):
S.info()
S.intrest()
print("\n")
Output :
I am a Student. My name is Sasi. I am 19 years old.
I want to learn Polymorphism
I am a Professor. My name is Venkatesh. I am 45 years old.
I can teach Polymorphism
I am a Student. My name is Mowli. I am 19 years old.
I want to learn Inheritance
I am a Professor. My name is Sharma. I am 38 years old.
I can teach Inheritance
As you can see here, we have created two classes Student
and Teacher
. They share a similar structure and have the same method names info()
and intrest
.
But if you notice that we have not created a common superclass or linked the classes together in any way.
Even then, we can pack these two different objects into a tuple and iterate through it using a common S
variable .
Which means Python can use two different class types, in the same way. We create a for loop that iterates through a tuple of objects.