Question: Feel free to use the exact code from slides 2 1 - 2 6 . Then, add an additional mammal to the animals.py file as

Feel free to use the exact code from slides 21-26.
Then, add an additional mammal to the animals.py file as well as make sure you update your polymorphism_demo file, the result should display the new animal name and its unique sound.
Please upload screen captures of the code and testing result for credit.
Let's create a new script and call it animals.pv
* The Mammal class represents a generic mammal.
class Mammal:
\# The __init__ method accepts an argument for
\# the mammal's species.
def __init__(self, species):
self.__species = species
\# The show_species method displays a message
\# indicating the mammal's species.
def show_spectes(self):
print('I am a', self.__species)
\# The make_sound method is the mammal's
\# way of making a generic sound.
def make_sound(self):
print('Grrrrr')
Now let's add the dog class to the animals.py file
\# The Dog class is a subclass of the Mammal Superclass.
class Dog(Mammal):
\# The __init__ method calls the superclass's
\# __init__ method passing 'Dog' as the species.
def __init__(self):
Mammal.__init__(self, 'Dog')
\# The make_sound method overrides the superclass's
\# make_sound method.
def make_sound(self):
print('Woof! Woof!')
Now let's create a new file called polymorphism_demo.py
This program demonstrates polymorphism.
import animals
def main():
```
# Create a Mammal object, a Dog object, and
# a Cat object.
mammal = animals.Mammal('regular animal')
dog = animals.Dog()
cat = animals.Cat()
```
\# Display information about each one.
print('Here are some animals and')
print('the sounds they make.')
print('-----------------------')
show_mammal_info(mammal)
print()
show_mammal_info(dog)
print()
show_mammal_info(cat) Now let's add an update to our polymorphism_demo file for testing:
```
show_mammal_info(dog)
print()
show_mammal_info(cat)
print()
show_mammal_info('bird')
* The show_mammal_info function accepts an object
* as an argument, and calls its show_species
* and make_sound methods.
def show_mammal_info(creature):
if isinstance(creature, animals.Mammal):
creature.show_species()
creature.make_sound()
else:
print('That is not a Mammal!')
```
Feel free to use the exact code from slides 2 1 -

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!