Hi Guys!!! Hope all is well...
Polymorphism is greek word Poly + Morphos = Many +forms
In programming, polymorphism means same function name (but different signatures) being uses for different types.
For clarification, Here implementing polymorphism in Python by using common names across methods in different classes rather than by using inheritance.
As for example we had a bunch of different classes representing animals, and each animal class had a method that’d print out the sound they make.
In our main()function, we have a variable called animals,which is a list of animal objects.
The question becomes, how could I iterate through each of the different objects and print out what sound they make?
One way we could do this is by adding a member variable called type that we’ll create and initialize in the constructor of each class. This variable will help us with the special case logic in our main()function. And when we run it, we’ll get the correct output.
However,this code works what we try to achieve but We have to write seems a lot of extra code . For What ? To call a method in each class that does the same thing.
Thanks
Saurabh Sharma
Happy Coding !!!
Polymorphism is greek word Poly + Morphos = Many +forms
In programming, polymorphism means same function name (but different signatures) being uses for different types.
For clarification, Here implementing polymorphism in Python by using common names across methods in different classes rather than by using inheritance.
As for example we had a bunch of different classes representing animals, and each animal class had a method that’d print out the sound they make.
In our main()function, we have a variable called animals,which is a list of animal objects.
The question becomes, how could I iterate through each of the different objects and print out what sound they make?
class Duck: | |
def quack(self): | |
print("a duck makes a quack sound") | |
class Cow: | |
def moo(self): | |
print("a cow makes a moo sound") | |
class Dog: | |
def bark(self): | |
print("a dog makes a woof sound") | |
def main(): | |
animals = [Duck(), Cow(), Dog(), Cow(), Duck(), Dog()] | |
for animal in animals: | |
#TODO want to print out the sound each of the different animals can make | |
main() |
class Duck: | |
def __init__(self): | |
self.type = "duck" | |
def quack(self): | |
print("a duck makes a quack sound") | |
class Cow: | |
def __init__(self): | |
self.type = "cow" | |
def moo(self): | |
print("a cow makes a moo sound") | |
class Dog: | |
def __init__(self): | |
self.type = "dog" | |
def bark(self): | |
print("a dog makes a woof sound") | |
def main(): | |
animals = [Duck(), Cow(), Dog(), Cow(), Duck(), Dog()] | |
for animal in animals: | |
animal_type = animal.type | |
if animal_type == "duck": | |
animal.quack() | |
elif animal_type == "cow": | |
animal.moo() | |
elif animal_type == "dog": | |
animal.bark() | |
else: | |
pass | |
However,this code works what we try to achieve but We have to write seems a lot of extra code . For What ? To call a method in each class that does the same thing.
And being programer = laziness, So We have to find better approach.
A better way to do this using polymorphism .
since the idea behind each method is printing out the sound each animal makes, would be to generalize the names of each method and call them the same thing, so:
This is what polymorphism is because we’re taking three different objects (Duck, Cow, and Dog), and we’re treating all of them in the same way through a common interface — being the make_sound() method.
A better way to do this using polymorphism .
since the idea behind each method is printing out the sound each animal makes, would be to generalize the names of each method and call them the same thing, so:
class Duck: | |
def make_sound(self): | |
print("a duck makes a quack sound") | |
class Cow: | |
def make_sound(self): | |
print("a cow makes a moo sound") | |
class Dog: | |
def make_sound(self): | |
print("a dog makes a woof sound") | |
def main(): | |
animals = [Duck(), Cow(), Dog(), Cow(), Duck(), Dog()] | |
for animal in animals: | |
animal.make_sound() |
This is what polymorphism is because we’re taking three different objects (Duck, Cow, and Dog), and we’re treating all of them in the same way through a common interface — being the make_sound() method.
Thanks
Saurabh Sharma
Happy Coding !!!